简体   繁体   English

Laravel,从select2中的数据库中获取选定的值

[英]Laravel, get selected values from database in select2

I am trying to show all selected values from database to multiple select2.So when I want to add other events I can add them. 我试图将所有选定的值从数据库显示到多个select2.so,当我想添加其他事件时,可以添加它们。

Offer tables:
id:1
title: event1
events: 17,6,8

column events has id of all events selected. 列事件具有选定的所有事件的ID。

This is what I do: 这是我的工作:

public function edit($id)
    {
        $offer=Offers::find($id);
         $events=Events::all();

        $explode=  explode(',', $offer->events);


        return view('edit',['offer'=>$offer,'events'=>$events,'explode'=>$explode]);
    }

Blade: 刀:

<div class="form-group m-form__group row">
    <label class="col-xl-3 col-lg-3 col-form-label">****</label>
    <div class="col-xl-9 col-lg-9">
        <select class="form-control m-select2" id="m_select2_3" name="events[]" multiple="multiple">
            <optgroup label="Events">
                @foreach($events as $event)
                    @foreach($explode as $item)
                        <option value="{{$event->id}}"  {{ $item == $event->id ? 'selected="selected"' : ''}}>{{$event->title}}</option>
                    @endforeach
                @endforeach
            </optgroup>
        </select>
    </div>
</div>

So when i select the value it shows three times repeated values 因此,当我选择该值时,它将显示三倍的重复值

在此处输入图片说明

You're loading all the database events in the $events variable and not the events associated with the offer. 您正在$events变量中加载所有数据库事件,而不是与商品关联的事件。 You should be doing something like this: 您应该这样做:

$events = Event::find(explode(',', Offer::find($id)->events));

Having said that, it would be best to use relationships for that instead of manually setting the ids https://laravel.com/docs/5.7/eloquent-relationships 话虽如此,最好是为此使用关系,而不是手动设置ID https://laravel.com/docs/5.7/eloquent-relationships

Maybe when you are getting events, you are getting duplicated, try this: 也许当您收到事件时,您会变得重复,请尝试以下操作:

public function edit($id)
    {
        $offer=Offers::find($id);
         $events=Events::all();

        $explode =  array_values(array_unique(explode(',', $offer->events)));



        return view('edit',['offer'=>$offer,'events'=>$events,'explode'=>$explode]);
    }

However, if that's the case, you should know that you offers table do not have duplicated event at the first place so you might want to change the script which saves events as well for the offer to remove duplicate entries. 但是,在这种情况下,您应该知道您的报价表首先没有重复的事件,因此您可能需要更改保存事件的脚本,以便报价删除重复的条目。

Also, I don't know if your "events" table contains duplicated events as well. 另外,我也不知道您的“事件”表是否也包含重复的事件。 Please check that and remove any duplicated values from your "events" table and if you cant then use same code for that as well like so in your controller: 请检查并从“事件”表中删除所有重复的值,如果不能,则在控制器中使用相同的代码:

$events = array_values(array_unique(Events::all()->toArray()));

I hope it helps 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM