Skip to content
Advertisement

Old value in multiple select option in laravel blade

Here is my select option

<select name="recomemded_food[]" value="" style="width:560px;" multiple class="chosen-select" >
<option value="American Black Bear">American Black Bear</option>
<option value="Asiatic Black Bear">Asiatic Black Bear</option>
<option value="Brown Bear">Brown Bear</option>
<option value="Giant Panda">Giant Panda</option>
</select>

And below is my code trying to use the foreach loop to get the array value. but I am receiving the following error:

@foreach (explode(',',old('recomemded_food')) as $recomemded_food) 
{{$recomemded_food}}
@endforeach

Error Message : explode() expects parameter 2 to be string

Advertisement

Answer

If you pass the select values from Controller:

$recommended_foods = ["American Black Bear",
                       "Asiatic Black Bear",
                       "Brown Bear",
                       "Giant Panda"];

and In the view:

<select required="required" class="form-control" name="recommended_food">
    @foreach ($recommended_foods as $key => $food)
        <option value="{{ $food}}" {{ (old("recommended_food") == $food ? "selected":"") }}>{{ $food }}</option>
    @endforeach
</select>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement