Home > database >  How can I edit multi select box in laravel?
How can I edit multi select box in laravel?

Time:09-23

BLADE FILE FOR EDIT

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

CONTROLLER

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

On Edit page, I want to pre-fill select box with user selected countries. Please help me with the same. Thanks in advance

CodePudding user response:

You can use it like this

<!-- 
* $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>

CodePudding user response:

As you've said, you're using Select2 for your multi-select box

Then you can do in your script:

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

Now if let's say you have a query for pre-selected countries, you can do this in your controller instead:

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

Then you can do in your script:

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