Laravel显示多个选定值

时间:2017-10-11 02:13:29

标签: php laravel

如何从多个选定值正确显示。我有可以连接到产品的供应商的表格,我使用implode来组合所选的值。我的问题是,只检索一个选中的值无法读取所选的其他值

$selected = array($products->supplier_id);

<select  name="supplier_id[]" multiple="multiple">
   @foreach($suppliers as $supplier)
         <?php  $isSelected = in_array($supplier->id,$selected) ? "selected='selected'" : ""; ?>
   <option {{ $isSelected }} value="{{ $supplier->id }}">{{ $supplier->name}}</option>
   @endforeach
</select>

2 个答案:

答案 0 :(得分:0)

试试这样:

$selected = explode(",", $products->supplier_id);

<select  name="supplier_id[]" multiple="multiple">
   @foreach($suppliers as $supplier)
     <option value="{{ $supplier->id }}" {{ (in_array($supplier->id, $selected)) ? 'selected' : '' }}>{{ $supplier->name}}</option>
   @endforeach
</select>

答案 1 :(得分:-1)

$array = explode(',',$drawings->machine_id);

{!! Form::select('machine[]', $machines, $array, ['class' => 'form-control select2', 'multiple', 'required' => '']) !!}

这就是我所做的,并且有效...