Here is my select option
I think the best answer to this question is this below
@if(count($categories) > 0)
@foreach($categories as $category)
<option value="{{ $category->id }}" {{ (old('lawyer_fields') == null ? '' : in_array($category->id ,old('lawyer_fields')) ? "selected":"") }}>
{{ $category->name }}
</option>
@endforeach
@else
<option disabled>بدون انتخاب</option>
@endif
This is the way I do, very dynamic.
<select id="gender" name="gender">
<option>Select</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
<script>
var currentGender = null;
for(var i=0; i!=document.querySelector("#gender").querySelectorAll("option").length; i++)
{
currentGender = document.querySelector("#gender").querySelectorAll("option")[i];
if(currentGender.getAttribute("value") == "{{ old("gender") }}")
{
currentGender.setAttribute("selected","selected");
}
}
</script>
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
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
@foreach ($desgInfo as $key => $value)
<option value="{{ $key }}"
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} />
{{ $value }}
</option>
@endforeach
</select>
During Edit
You need to add
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}
{{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}
which will look like
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
@foreach ($desgInfo as $key => $value)
<option value="{{ $key }}"
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}
{{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}
/>
{{ $value }}
</option>
@endforeach
</select>
Select all selected id's in a multiselect dropdown in laravel 5.4 with harvest chosen
Thanks
For me Jilson Thomas's answer did not work for multiple select so I changed it to:
{{ in_array($food, old("recommended_food")) ? "selected":"") }}
Best way:
<label for="name">Tags</label>
<select name="tags[]" class="form-control select-tag" multiple>
@foreach($tags as $tag)
<option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected": ""}}>{{$tag->name}}</option>
@endforeach
</select>
I was parsing JSON data, and was faced with repopulating the multi-select as an array, and only wanted one table, and wasn't using keys since this array was being passed back from the show method in the controller, so had to supply the values directly on the template, in my case as a crud resource on the edit.blade.php template (also on the create template).
This worked for me, and was the cleanest I could get it on the view.
<select id="recommended_food" multiple="multiple" size=3 style='height: 100%;' name="recommended_food[]">
@if ($food->recomemded_food)
{{ ($val = 'Fries')
&& $chosen = in_array($val, $food->recommended_food)
? 'selected':null}}
<option value="{{$val}}" {{$chosen}}>{{$val}}</option>
{{ ($val = 'Hot Dogs')
&& $chosen = in_array($val, $food->recommended_food)
? 'selected':null}}
<option value="{{$val}}" {{$chosen}}>{{$val}}</option>
{{ ($val = 'Hamburgers')
&& $chosen = in_array($val, $food->recommended_food)
? 'selected':null}}
<option value="{{$val}}" {{$chosen}}>{{$val}}</option>
@endif
</select>