how to store dropdown multiple value in laravel

How To Store Multiple Select Value In Laravel?

In this tutorial, I will give you a simple example of Store Multiple Select Value In Laravel, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

Sometimes we need to store multiple selected dropdown values in the database in Laravel Application. We simply store our multiple selected values as a Comma Separated using Implode Function.

Implode Function

The implode() function in PHP is a predefined, built-in, binary-safe function in PHP, Implode function converts “array to a string,” Implode function takes an array of elements and returns a string.

Parameters

The implode() function accepts two parameters. And, out of which, one parameter is optional, and the other is mandatory.

  1. The first parameter is Separator.
  2. The second parameter is Array.

Syntax

implode(separator,array)

If you want to Read more About PHP Implode Function Click here.

So let’s see the below example, which will help you a lot in Laravel Project.

Blade File

In the Blade file, We can select single or multiple months, We assign the name as name=”month[]”, We want to Store multiple Months into a Single Column in the Database.

<div class="col-md-4">
   <div class="form-group">
      <label>{{ __('Month') }}</label>
      <select class="form-control" multiple data-live-search="true" name="month[]">
         <option value="1">January</option>
         <option value="2">February</option>
         <option value="3">March</option>
         <option value="4">April</option>
         <option value="5">May</option>
         <option value="6">June</option>
         <option value="7">July</option>
         <option value="8">August</option>
         <option value="9">September</option>
         <option value="10">October</option>
         <option value="11">November</option>
         <option value="12">December</option>
      </select>
   </div>
</div>
How To Store Multiple Select Value In Laravel

Controller

We have a Controller, and we have Store Function in given Controller, Where we write logic to insert Post array data and store as Comma Separate in month Column.

    public function Store(Request request)
    {
      //Store Month Post Request Data into $getMonthReq Variable.
     $getMonthReq = $request->month;
     $months = implode(',', $getMonthReq);

        $data = new ModelName();
        $data->month = $months;
        $data->save();
        dd('Data Successfully Added');
     }

Output

You can see the output, We Successfully Stored Month Array Data in Column as Comma Separated values in the table.

how to store dropdown multiple value in laravel

In this article, we learned How To Store Multiple Select Value In Laravel, I hope this article will help you with your Laravel Application Project.

Also Read: Insert Data using Ajax in Laravel.