skip to Main Content

How to keep old value in select option when I updated the form?

Form:

<select class="form-select" name="slug" id="slug" value="{{$project->slug ?? '' }}">
<option value="">Select Category </option>
<option value="commercial">commercial</option>
</select>

2

Answers


  1. You can use old() like this

    <select class="form-select" name="slug" id="slug">
        <option value="commercial" {{ old('slug', $project->slug) == 'commercial' ? 'selected' : '' }}>Commercial</option>
        <option value="another" {{ old('slug', $project->slug) == 'another' ? 'selected' : '' }}>Another</option>
        <option value="anotherrr" {{ old('slug', $project->slug) == 'anotherrr' ? 'selected' : '' }}>Anotherrr</option>
    </select>
    
    Login or Signup to reply.
  2. Simply use @selected(bool), and if the condition is true it will return selected.

    <select class="form-select" name="slug" id="slug">
        <option value="commercial" @selected(old('slug', $project->slug) == 'commercial')>Commercial</option>
        <option value="another" @selected(old('slug', $project->slug) == 'another')>Another</option>
    </select>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search