<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->text('cat');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('posts');
}
}
C:\xampp\htdocs\reset\app\Models\Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
use HasFactory;
protected $fillable = ['name', 'cat', 'description'];
/**
* Set the categories
*
*/
public function setCatAttribute($value) {
$this->attributes['cat'] = json_encode($value);
}
/**
* Get the categories
*
*/
public function getCatAttribute($value) {
return $this->attributes['cat'] = json_decode($value);
}
}
C:\xampp\htdocs\reset\routes\web.php
<?php
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('postCreate', [PostController::class, 'postCreate']);
Route::post('postData', [PostController::class, 'postData'])->name('postData');
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller {
public function postCreate() {
return view('post');
}
public function postData(Request $request) {
$input = $request->all();
Post::create($input);
dd('Post created successfully.');
}
}
By Hardik Savani June 16, 2020 Category : LaravelPlayUnmuteLoaded: 1.15%FullscreenI am going to show you example of how to store multiple select values in laravel. i explained simply about how to store multiple select values in database using laravel. i explained simply about laravel store multiple select dropdown list values. we will help you to give example of how to save multiple select box value in laravel 6, laravel 7, laravel 8 and laravel 9.
Sometime we need to store multi select dropdown values in database using laravel. we can take json data type or text data type of that field so we can json_encode our array and store it into database.
Here, i will give you very simple and step by step example that will explain you how to store multiple select values in laravel using Accessors & Mutators. so let's see bellow example and bellow preview:
Preview:
Step 1: Create Migration
Here, in this example you need to create posts table with name, description and cat column. cat column has text or json data type. as bellow created:
<?php use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('description'); $table->text('cat'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); }}
Now we can run migration using bellow command:
php artisan migrate
Step 2: Create Post Model
Here, we will create Post model with Accessors & Mutators, so when you store categories then it will make json_encode() and when you get then json_decode(). so let's see bellow code:
app/Post.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model{ protected $fillable = ['name','cat','description']; /** * Set the categories * */ public function setCatAttribute($value) { $this->attributes['cat'] = json_encode($value); } /** * Get the categories * */ public function getCatAttribute($value) { return $this->attributes['cat'] = json_decode($value); }}
Here, we will simple create PostController with two method that assign with route. so let's add two routes:
app/Http/Controllers/PostController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request;use App\Post; class PostController extends Controller{ public function postCreate() { return view('post'); } public function postData(Request $request) { $input = $request->all(); Post::create($input); dd('Post created successfully.'); }}
Step 5: Create Blade File
In this step, we will create blade file post.blade.php file there we added form code with multi select dropdown box. so let's put bellow code: