Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mgolami's avatar

keep selected value in select control after page refresh in laravel

I am a beginner in Laravel in my blade :

<select id="Priority_id" style="width:180px;" class="myform-select">
    <option value="" selected>Select priority</option>
     @foreach ($priorities as $priority)
          <option value="{{ $priority->id }}">
              {{ $priority->Desc }}
          </option>
     @endforeach
</select>

to keep old value of select i find two way :first change option to this:

<select id="Priority_id" style="width:180px;" class="myform-select">
    <option value="" selected>Select priority</option>
    @foreach ($priorities as $priority)
           <option value="{{ $priority->id }}" {{ ( old('Priority_id')==$priority->id ? "selected":"") }}>
                    {{ $priority->Desc }}
           </option>
    @endforeach
</select>

second change to it:

<select id="Priority_id" style="width:180px;" class="myform-select">
        <option value="" selected>انتخاب نمائید</option>
        @foreach ($priorities as $priority)
                    <option value="{{ $priority->id }}" @if (old('Priority_id') == $priority->id) selected="selected" @endif>
                               {{ $priority->Desc }}
                    </option>
       @endforeach
 </select>

As a result, when I refresh the page, I don't get results in both methods and I lose the previous value.

0 likes
3 replies
Snapey's avatar

The old() helper only returns the value selected before a validation error occurred.

If you have a default value then you add it as the second parameter for old

You also need to name your input field. I would stick to the same naming convention as the db column that the result goes into.

Finally, you can use @selected blade helper so that you end up with something like;

<select id="Priority_id" style="width:180px;" class="myform-select" name="Priority_id">
    <option>Select priority</option>
    @foreach ($priorities as $priority)
           <option value="{{ $priority->id }}" @selected( old('Priority_id', $model->priority)==$priority->id) >
                    {{ $priority->Desc }}
           </option>
    @endforeach
</select>
1 like
Snapey's avatar

@mgolami no, you have to reference the place where the current select state is stored.

Please or to participate in this conversation.