Laravel 5.2如何使用多项选择填充编辑表单? [英] Laravel 5.2 How to populate edit form with multiple select?

查看:12
本文介绍了Laravel 5.2如何使用多项选择填充编辑表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含较少城市的区域。因此,我决定使用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>

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

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

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

关系为一区多城

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

    public function cities(){
        return $this->hasMany(City::class);    
    }
}

该城市属于区域

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

    public function zone(){
        return $this->belongsTo(Zone::class);    
    }
}

这是我的编辑方法

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

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

<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>

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

推荐答案

在我看来就是这样

<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>

在我的控制器中就像这样

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'));
    }

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

这篇关于Laravel 5.2如何使用多项选择填充编辑表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆