How to Show the Old Values in Multiple Select Options in Laravel
In this lesson, we will see how to show the old values in multiple select options when editing in Laravel, this approach will make the user see his previously selected options when updating the form.
Display the old values in multiple select options in Laravel
To do that let's assume that we are working on an e-commerce project and each product has many colors and we have multiple select options for colors see the example below:
<div class="mb-3"> <label for="color_id" class="form-label">Colors*</label> <select class="form-control" name="color_id[]" id="color_id" multiple > @foreach ($colors as $color) <option value="{{$color->id}}" @if(collect(old('color_id',$product->colors->pluck('id')))->contains($color->id)) selected @endif> {{ $color->name }} </option> @endforeach </select></div>