mayuri0606's avatar

Cannot get old values of multi-select dropdown in laravel blade

I want to get the selected old values of multi-select dropdown in the edit.blade.php. (It means once I selected team names from the multiselect dropdown when creating an employee, I want to get the selected multiselect dropdown value in the edit form. Currently multiselct drop down comes empty though I selected team names when creating an employee)

blade.php

<div class="form-group">
  <select name="team[]" id="team" class="selectpicker" multiple>
    @foreach ($tdropdown as $tdrop =>$id) 
      <option value="{{$id}}">{{$tdrop}}</option>   
    @endforeach
  </select>
</div>

I tried with many ways by by following https://stackoverflow.com/questions/35611945/old-value-in-multiple-select-option-in-laravel-blade, but failed. Thank you for any help.

0 likes
20 replies
Wakanda's avatar
<div class="form-group">
  <select name="team[]" id="team" class="selectpicker" multiple>
    @foreach ($tdropdown as $tdrop =>$id) 
      <option value="{{$id}}" {{ (old("team[]") == $tdrop ? "selected":"") }}>{{$tdrop}}</option>   
    @endforeach
  </select>
</div>
mayuri0606's avatar

@loyd I tried with your code. But still same result. The selected values doesn't come in the edit form.

mayuri0606's avatar

I tried with this way too. But didn't work for me.

<option value="{{$id}}" {{ (collect(old('team'))->contains($id)) ? 'selected':'' }}>{{$tdrop}}</option>  

Any idea?

MichalOravec's avatar

This will work.

<div class="form-group">
    <select name="team[]" id="team" class="selectpicker" multiple>
        @foreach ($tdropdown as $tdrop => $id) 
            <option value="{{ $id }}" {{ in_array($id, old('team')) ? 'selected' : '' }}>{{ $tdrop }}</option>   
        @endforeach
    </select>
</div>

Because you use as value $id, so you have to compare $id.

mayuri0606's avatar

@michaloravec I tried with your code. But it still doesn't display the multi-select drop down selected names in the edit form. When I go to the edit mode of a user, it remains as nothing is selected from the multi-select drop down. But the multi-select drop down selected names are already saved in the database.

MichalOravec's avatar

@mayuri0606 old() is used for getting selected values when you validation fails.

If you want to get selected from your database you need to compare it with that data in the database.

<div class="form-group">
    <select name="team[]" id="team" class="selectpicker" multiple>
        @foreach ($tdropdown as $tdrop => $id) 
            <option value="{{ $id }}" {{ $employee->teams->contains($id) ? 'selected' : '' }}>{{ $tdrop }}</option>   
        @endforeach
    </select>
</div>
1 like
Snapey's avatar

But the multi-select drop down selected names are already saved in the database

the names are stored or the ids?

You have to compare if the model contains the select element that you are currently iterating over

it will only work if you are comparing the same things, keys or values.

Snapey's avatar

but if validation fails (elsewhere) then the select list will revert to its previous values because you don't use the old helper

1 like
MichalOravec's avatar
Level 75

@snapey Yes, you are right.

@mayuri0606 This is the combination with old when your validation fails.

<div class="form-group">
    <select name="team[]" id="team" class="selectpicker" multiple>
        @foreach ($tdropdown as $tdrop => $id) 
            @if (old('team'))
                <option value="{{ $id }}" {{ in_array($id, old('team')) ? 'selected' : '' }}>{{ $tdrop }}</option>   
            @else
                <option value="{{ $id }}" {{ $employee->teams->contains($id) ? 'selected' : '' }}>{{ $tdrop }}</option>
            @endif 
        @endforeach
    </select>
</div>
1 like
Snapey's avatar

its simpler than that, you can pass the teams as the second parameter to old

more like

<option value="{{ $id }}" {{ old('teams',$employee->teams)->contains($id) ? 'selected' : '' }}>{{ $tdrop }}</option>

but I cant remember if old returns a collection or plain array. It may be you have to use array method rather than contains

1 like
peymanator's avatar

This is cleaner and better way:

{{ ( in_array($id, old('team')?: [])) ? 'selected' : '' }}

because in_array expect array and when in first place you haven't any old values the old() will return null which is not in_array except. to avoid this put another condition to check if old() is empty return and empty array

Snapey's avatar

@peymanator how does that work with values you already have? You no longer have access to the EXISTING values.

babai9's avatar

Can somebody answer if the answer which is marked as correct will work if you need to do something that if (old('team')) has value should get selected if not old value or select blank then no option will get selected as old will be empty but when you will reload the page then it will show the database selected option. I am stuck here, can somebody please help?

Snapey's avatar

@babai9 without knowing your field names or how you are doing the loop, noone can suggest working code.

If your select input is a multiple input then old('team') will be an array and you must check each option in_array(old('team')) and if it is in the array then add selected to the option

Does that help or is my answer to vague?

babai9's avatar

@Snapey not sure if we take this question as an example the answer that is marked correct is right and its okay the problem that I tried to find out is that when you are editing the select @if (old('team') .... it means the old value has got something @else .... this part is of the database array suppose, everything is fine till this but what to do when old value will be empty, i mean suppose the first time edit page will open with the database values selected fine, now if I change the value then it will take the old one fine but now if I remove all of what I have chosen then the select should be empty, this is what I am looking for how to make the select the empty for that time as I need to show the select empty, the value I am getting while submitting is empty its okay but I need to show the select empty. The same thing I got stuck with a checkbox this is my question https://laracasts.com/discuss/channels/laravel/old-value-of-checkbox-with-the-one-from-the-database-along-with-if-old-remains-empty-laravel

Please or to participate in this conversation.