Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this tutorial, I will give you a simple example of How to show multiple selected values 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 and show these multiple selected values in Blade File in Laravel Application.
We simply store our multiple selected values as a Comma Separated using Implode Function and get the column data with the help of Explode Function in Laravel application.
If you want to Store Multiple Select Dropdown Value In Laravel follow this Article : Click Here.
The explode() function breaks a string into an array, This function is used to split a string into different strings, using this function we create an array from a string, Explode function is binary-safe.
The explode function accepts three parameters of which two are compulsory and one is optional.
array explode(separator,OriginalString,NoOfElements)
After Store Multiple Select Values Into the Database, The Column will look like this.
We have a Controller, and we have Edit Function in the given Controller, Where we write logic to call column values array data and show it into Blade File (edit.blade.php) with the help of Explode Function.
public function Edit($id)
{
$data = ModelName::find($id);
$SelectedMonths = explode(',', $data->month);
return view('package.edit', ['data' => $data,'months' => $SelectedMonths]);
}
We pass the “$months” Variable from Controller, To show multiple Dropdown In the edit Blade file in Laravel.
We use the “in_array” Function to check which values are selected in Array in Blade File.
<div class="col-md-4">
<div class="form-group">
<label>{{ __('Month') }}</label>
<select class="form-control select2" multiple data-live-search="true" name="month[]" required>
<option value="1" @if(in_array('1',$months)) selected @endif>January</option>
<option value="2" @if(in_array('2',$months)) selected @endif>February</option>
<option value="3" @if(in_array('3',$months)) selected @endif>March</option>
<option value="4" @if(in_array('4',$months)) selected @endif>April</option>
<option value="5" @if(in_array('5',$months)) selected @endif>May</option>
<option value="6" @if(in_array('6',$months)) selected @endif>June</option>
<option value="7" @if(in_array('7',$months)) selected @endif>July</option>
<option value="8" @if(in_array('8',$months)) selected @endif>August</option>
<option value="9" @if(in_array('9',$months)) selected @endif>September</option>
<option value="10" @if(in_array('10',$months)) selected @endif>October</option>
<option value="11" @if(in_array('11',$months)) selected @endif>November</option>
<option value="12" @if(in_array('12',$months)) selected @endif>December</option>
</select>
</div>
</div>
Now you can see the output, We displayed all the selected dropdown multiple values in Edit Blade File.
In this article, we learned “How to show multiple selected values in Laravel”, I hope this article will help you with your Laravel Application Project.