Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
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.
The implode() function accepts two parameters. And, out of which, one parameter is optional, and the other is mandatory.
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.
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>
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');
}
You can see the output, We Successfully Stored Month Array Data in Column as Comma Separated values in the table.
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.