问题
I have a dropdown called designation, where a user can add some information against multiple designation. If I add 1 record against 3 designation, then I need to select those during validation and edit time also.
Ex: Choosed id's {5,7,8} from [1 to 10].
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
@foreach ($desgInfo as $key => $value)
<option value="{{ $key }}" {{ old('forWhom',$info->forWhom) == $key ? 'selected' : ''}} />{{ $value }}</option>
@endforeach
</select>
After add of those information I store those selected id's in comma(,) separator i.e 5,7,8.
How can I make select this in laravel 5.4
回答1:
After playing around a bit, I got the result.
Here is the piece of code.
During Add
<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 Suppose you got the result of selected ids in
$info->forWhom
<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>
I hope this will help some one else.
来源:https://stackoverflow.com/questions/44369072/select-all-selected-ids-in-a-multiselect-dropdown-in-laravel-5-4-with-harvest-c