【发布时间】:2021-08-16 19:19:40
【问题描述】:
我正在尝试从选择选项中存储多选值,我可以保存,但是当我从数据库中获取数据时,它在选择中选择了错误的选项;
category = [{'id' : "1", 'name'=>'A'},{'id' : "2", 'name'=>'B'},{'id' : "3", 'name'=>'C'},{'id' : "4", 'name'=>'D'},{'id' : "5", 'name'=>'E'}]
parent_category = [1,2,3](comming from database)
刀片文件:
<select class="js-example-basic-multiple" id="category" name="parent_id[]" multiple>
<option value=0>None</option>
@if($categories)
@foreach($categories as $category)
<?php $dash=''; ?>
<option value="{{$category->id}}" @if(in_array($category->id, explode(',',$products->parent_id))) selected @endif>{{$category->name}}</option>
@if(count($category->subcategory))
@include('pages.admin.category.sub-category-list',['subcategories' => $category->subcategory,'products'=>$products])
@endif
@endforeach
@endif
</select>
子类别列表.blade.php
<?php $dash.='-- '; ?>
@foreach($subcategories as $subcategory)
<option value="{{$subcategory->id}} " @if(in_array($subcategory->id, explode(',',$products->parent_id))) selected @endif>{{$dash}}{{$subcategory->name}}</option>
@if(count($subcategory->subcategory))
@include('subCategoryList-option',['subcategories' => $subcategory->subcategory,'products'=>$products])
@endif
@endforeach
如图选择值正确但数据选择值错误
【问题讨论】:
-
$products->parent_id应该给什么? -
列出特定产品的位置