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)
@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.
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
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?
@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
@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