如何在 laravel 中编辑多个 select 框?

How can I edit multi select box in laravel?

BLADE 编辑文件

<div class="form-group mb-3">
                        <label>Country:</label>
                        <div class="col-sm-4">
                        <select  id="country-dd" name="country[]" class="js-example-basic-multiple form-control" multiple="multiple">
                        @foreach($countries as $country)
                        <option value="{{$country->id }}" {{$country->id == $user->country ? 'selected' : '' }}> {{$country->name}}</option>
                        @endforeach
                        </select>
                    </div>
                    </div>

控制器

public function edituser(Request $request, $id = null){
        $this->authorize('Admin');
        $userstateid = [];
        $user = new User;
        $user = $user->where('id', $id)->first();
        $countries = Country::get();
        $states = State::get();
        $cities = City::get();
        $roles = Role::get();
        //dd($user);
        return view('edituser',compact('user', 'countries', 'states', 'cities', 'roles'));
    } 

在“编辑”页面上,我想 pre-fill select 框中包含用户 selected 国家。请帮我做同样的事情。提前致谢

你可以这样使用它

<!-- 
* $countries variable contents all the country name/id
* $user->country contains all the country id selected by user at the time of creation,
  so it should be an array or json string (if then decode it to array first json_decode())
-->

<div class="form-group mb-3">
    <label>Country:</label>
    <div class="col-sm-4">
    <select  id="country-dd" name="country[]" class="js-example-basic-multiple form-control" multiple="multiple">
    @foreach($countries as $country)
    <option value="{{$country->id }}" {{is_array($user->country) && in_array($country->id, $user->country) ? 'selected' : '' }}> {{$country->name}}</option>
    @endforeach
    </select>
</div>
</div>

如您所说,您正在使用 Select2 作为多select 框

然后你可以在你的脚本中做:

<script>
    var preselected_countries = [
        {id: 1, text: 'USA'},
        {id: 2, text: 'Canada'},
        {id: 3, text: 'Brazil'},
    ];
    $('country-dd').select2('data', preselected_countries);
</script>

现在,如果假设您要查询 select 之前的国家/地区,您可以改为在控制器中执行此操作:

public function edituser(Request $request, $id = null){
        $this->authorize('Admin');
        $userstateid = [];
        $user = new User;
        $user = $user->where('id', $id)->first();
        $countries = Country::get();
        $userCountry = Country::find($user->country); //assuming your country is just a field and contains only a single value and not an array
        $preselectedCountry = [
            (object) [
                'id' => $userCountry->id,
                'text' => $userCountry->whateverColumnTheNameIs
            ]
        ];
        $states = State::get();
        $cities = City::get();
        $roles = Role::get();
        //dd($user);
        return view('edituser',compact('user', 'countries', 'states', 'cities', 'roles', 'preselectedCountries'));
    }

然后你可以在你的脚本中做:

<script>
    var preselected_countries = {{$preselectedCountries}};
    $('country-dd').select2('data', preselected_countries);
</script>