How to Show Old Value of Select Box in Laravel
In this lesson, we will see how to show the old value of the select box in Laravel, let's assume that we are fetching categories from the MySQL database and we display them in a select box once the form is submitted if there are any validation errors we want to keep the old value of the select box.
Keep the old value of the select box
So to show the old category selected before submitting the form use the code below.
<select class="form-control shadow-none" name="category_id">
<option selected disabled>Choose a category</option>
@foreach ($categories as $category)
<option value="{{$category->id}}"
@if(old('category_id') == $category->id) selected @endif>{{$category->name}}</option>
@endforeach
</select>