Laravel Advance | How to Store Multiple Select Values in Laravel?

Raviya Technical
3 min readJan 11, 2022

I am going to show you an example of how to store multiple select values in laravel. I explained simply how to store multiple select values in a database using laravel. I explained simply about laravel store multiple select dropdown list values. we will help you to give examples of how to save multiple select box values in laravel 6, laravel 7, and laravel 8.

Sometimes we need to store multi-select dropdown values in the 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 the database.

Step 1: Create Migration

Here, in this example, you need to create a posts table with the name, description, and cat column. cat column has text or JSON data type. as bellow created:

database/migrations/2020_06_13_102114_create_posts_table.php

<?phpuse 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 the below command:

php artisan migrate

Step 2: Create Post Model

Here, we will create a 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

<?phpnamespace 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);}}

Step 3: Create Routes

Here, we will simply create two routes one get a route to access view and another post route for store data into the database. so let’s add two routes:

routes/web.php

Route::get('postCreate','PostController@postCreate');Route::post('postData','PostController@postData')->name('postData');

Step 4: Create Controller

Here, we will simply create PostController with two methods that assign with the route. so let’s add two routes:

app/Http/Controllers/PostController.php

<?phpnamespace 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 the multi-select dropdown box. so let’s put bellow code:

resources/views/post.blade.php

<html><head><title>How to Store Multiple Select Values in Database using Laravel? - ItSolutionStuff.com</title><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"></head><body><div class="container"><div class="row"><div class="col-md-8 offset-2 mt-5"><div class="card"><div class="card-header bg-info"><h6 class="text-white">How to Store Multiple Select Values in Database using Laravel? - ItSolutionStuff.com</h6></div><div class="card-body"><form method="post" action="{{ route('postData') }}" enctype="multipart/form-data">@csrf<div class="form-group"><label>Name</label><input type="text" name="name" class="form-control"/></div><div class="form-group"><label><strong>Description :</strong></label><textarea class="ckeditor form-control" name="description"></textarea></div><div class=""><label><strong>Select Category :</strong></label><br/><select class="form-control" name="cat[]" multiple=""><option value="php">PHP</option><option value="react">React</option><option value="jquery">JQuery</option><option value="javascript">Javascript</option><option value="angular">Angular</option><option value="vue">Vue</option></select></div><div class="text-center" style="margin-top: 10px;"><button type="submit" class="btn btn-success">Save</button></div></form></div></div></div></div></div></body></html>

Now we are ready for the app.

You can run the application using bellow command:

php artisan serve

I hope it can help you…

--

--