In this tutorial we will show you how to retain Old value in multiple select option in laravel blade file. More then times we need select box in form where user can select multiple options and retain old value after validation in laravel major periority any admin panel.
This example help you laravel select option selected from database and Option value selected in laravel blade. We have already describe Retain Old Value on validation Error in Laravel, but here we implement how to retain multiple select box old value if validation occur on other field in laravel.
Old Values in Multiple Select Option in Laravel
The best example code that you need to append in your option tag:
{{in_array($tag->id, old("tags") ?: []) ? "selected" : ""}}
Here the tags are an array and user can select more then one tags in one time if the validation comes other fields then we need to retain the selected tags which user added in select box.
<div class="form-group">
<strong>Tags:</strong>
<select name="tags[]" class="form-control custom-select" multiple>
<option value="">Select Tags</option>
@foreach($tags as $tag)
<option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected" : ""}}>{{$tag->name}}</option>
@endforeach
</select>
</div>
I hope if condition with old value in multiple select option in laravel blade work for you. If you have any question or want to learn something else let me know in comment box.
1 thought on “Retain Old value in Multiple Select Option in Laravel”