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
If you pass the select values from Controller:
and In the view:
I was parsing
JSON
data, and was faced with repopulating the multi-select as anarray
, and only wanted one table, and wasn't usingkeys
since thisarray
was being passed back from theshow
method in thecontroller
, so had to supply the values directly on the template, in my case as a crud resource on theedit.blade.php
template (also on thecreate
template).This worked for me, and was the cleanest I could get it on the view.
For me Jilson Thomas's answer did not work for multiple select so I changed it to:
I think the best answer to this question is this below
After Playing around a bit I came up with this and it seems to work just splendidly
I may be 100% wrong in leveraging the collect function but it works fine on many of my tests. I want to say thank you to MPS as it was your example that really made me try this as when there is no data in "recommended_food" you will pull an error because in_array requires an array and will not work well with null so I then came up with the idea of doing something like this.
inline but man that looks ugly to me so long story short I am using the following instead
Hope this helps others!!
The question was asked for multiple chosen.
Assuming you have a field called designation and you need to choose multiple designation for adding 1 record and the field name is forWhom
During Add
You need to add this {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} peace of code to your option tag
which will look like
During Edit
You need to add
which will look like
Select all selected id's in a multiselect dropdown in laravel 5.4 with harvest chosen
Thanks