1

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

4

3 Answers 3

0

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>
2
  • He's using Select2. That won't work.
    – rkg
    Sep 23, 2021 at 7:38
  • I'm sorry @rkg but that works fine. Sep 23, 2021 at 7:49
0

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>
1
  • he wants to show all the country in the option and pre-select all the options that has been selected while creating / initially stored. Sep 23, 2021 at 7:51
0

the simplest code worked with me,

<select class="form-control select" name="country[]" multiple>
 @foreach (getCountries() as $key => $item)
   @if(\Str::contains($task->options->require_countries, $key)) 
    <option value="{{ $key }}" selected>{{ $item['country'] }}</option>
   @else
    <option value="{{ $key }}">{{ $item['country'] }}</option>
   @endif
 @endforeach
</select>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.