Laravel 5.2 如何使用多选填充编辑表单?

Dar*_*ber 1 laravel jquery-select2

我想创建一个包含几个城市的区域。所以我决定使用 jQuery Select2

这是我的创建表单多项选择

<div class="form-group {{ $errors->has('cities') ? 'has-error' : '' }}">
                    <label>Tentukan Kota</label>
                    <select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
                    @foreach($cities as $city)
                    <option value="{{ $city->id }}">{{ $city->name }}</option>
                    @endforeach
                    </select>

                </div>
Run Code Online (Sandbox Code Playgroud)

我可以像在文档中一样进行多项选择。

这是我的控制器,它处理显示创建表单

public function zone_create()
{
    $cities = City::where('zone_id', null)->get();
    return view('backend.admin.pricings.zone_create', compact('cities'));
}
Run Code Online (Sandbox Code Playgroud)

这种关系是一个区域有多个城市。

class Zone extends Model
{
    protected $fillable = [
        'name',    
    ];

    public function cities(){
        return $this->hasMany(City::class);    
    }
}
Run Code Online (Sandbox Code Playgroud)

城市所属区域

class City extends Model
{
    protected $fillable = [
        'zone_id',
        'name',    
    ];

    public function zone(){
        return $this->belongsTo(Zone::class);    
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的编辑方法

public function edit($id)
{
    $zone = Zone::find($id);
    $cities = City::all();
    return view('backend.admin.pricings.zone_edit', compact('zone', 'cities'));
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的编辑表格

<div class="form-group {{ $errors->has('cities') ? 'has-error' : '' }}">
                    <label>Tentukan Kota</label>
                    <select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
                    //load option from cities table
//set selected the city belongs to zone
//the other city which don't belong to zone still here for option
                    </select>

                </div>
Run Code Online (Sandbox Code Playgroud)

但是,我如何使用属于区域的城市填充我的编辑表单(多选)?

Dar*_*ber 7

在我看来就是这样

<select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
                    @foreach($cities as $city)
                        @if(in_array($city->id, $zoneCityIds))
                        <option value="{{ $city->id }}" selected="true">{{ $city->name }}</option>
                        @else
                        <option value="{{ $city->id }}">{{ $city->name }}</option>
                        @endif 
                    @endforeach
                    </select>
Run Code Online (Sandbox Code Playgroud)

像这样在我的控制器中

public function zone_edit($id)
    {
        $zoneCityIds = [];
        $zone = Zone::find($id);
        $cities = City::all();
        foreach($zone->cities as $zoneCity)
        {
            $zoneCityIds[] = $zoneCity->id;
        }        

        return view('backend.admin.pricings.zone_edit', compact('zone', 'cities', 'zoneCityIds'));
    }
Run Code Online (Sandbox Code Playgroud)

实际上这只是关于选项标签 selected="true"