php - Multiselect Box with Laravel and values from database
问题描述
I am having difficulty with multi-select boxes with Laravel, particularly, reading values from the database and displaying these values so they can be edited in my edit view.
In my controller, in the store function, I am using the implode function to save positive integers into a database field.
Here is the implode function
$prodmulti = implode(',', $prodmulti);
The database stores these values in a cell such as
prodmulti
1,2,3
This works fine and I get the cell, populated with values fine. The problem is when I wish to edit these values. If I have nothing saved in this cell, then as expected, I have nothing in the multi-select box which is good. If I have 1 number in the database cell, then again, it is fone, I can see the corresponding item in the multi-select box. If I have 2 or more items in the database cell, then the multi-select box shows as blank in the edit view. This is where I am having my problem.
The view blade for the edit page with the multi-select box looks like this:
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" name="prodmulti[]" style="width: 100%;" multiple>
<option></option>
@foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}" {{ $user->prodmulti == $productcategory->id ? "selected" : ""}}>
{{ $productcategory->name }}
</option>
@endforeach
</select>
</div>
</div>
And the show function in the controller looks like this where I use the explode to extract the values as an array as such.
$prodmulti = explode(',', $prodmulti);
return view(
'admin.users.edit',
compact([
'user',
'roles',
'countries',
'states',
'productcategories',
'prodmulti'
])
);
If it helps, here is my create a view where I again use one of these multi-select boxes to add values
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" id="prodmulti" name="prodmulti[]" style="width: 100%;" data-placeholder="Choose many.." multiple>
<option></option>
@foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}">
{{ $productcategory->name }}
</option>
@endforeach
</select>
</div>
</div>
Finally, I am using select2 multi-select boxes, but I get the same result if I use just standard bootstrap multi-select boxes and have tried both, I just like the look of the select2 multi-select boxes.
Any help would be greatly appreciated, Thanks, Steve.
解决方案
use in_array()
to check
$prodmulti = explode(',', $prodmulti);
return view('admin.users.edit', compact(['user', 'roles', 'countries', 'states', 'productcategories', 'prodmulti']));
then in blade
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" id="prodmulti" name="prodmulti[]" style="width: 100%;" data-placeholder="Choose many.." multiple>
<option></option>
@foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}"
{{ in_array($productcategory->id,$prodmulti) ? "selected" : "" }}>
{{ $productcategory->name }}
</option>
@endforeach
</select>
</div>
</div>
推荐阅读
- php - 如何使用外键打印表格
- android - 模拟器:错误:无法从内核映像文件中获取内核版本
- c - 重命名函数并确定其工作方式
- nodes - 如何使用 bc 从头节点到客户端节点
- javascript - 使用 router.put 和 router.delete 的 PUT 和 GET 方法不起作用(快递)
- java - 更新 MutableLiveData 时可观察到的抛出异常
- terraform - Terraform 配置程序 - 无法读取 ssh 私钥:找不到密钥
- amazon-dynamodb - 如何静默运行 aws cli 命令以创建 dynamodb 表
- entity-framework-core - 使用 EF Core 5 将 SQL Server 表拆分为多个文件
- c# - WhenActivated 在 ReactiveUI 内部实际上是如何工作的?