Select all selected id's in a multiselect dropdown in laravel 5.4 with harvest chosen

可紊 提交于 2019-12-22 01:07:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!