首页 > 解决方案 > 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.

标签: phphtmllaravel

解决方案


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>

推荐阅读