Setting selected option in laravel form

Setting selected option in laravel form

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Selecting Options in Laravel Forms

When building dynamic forms in Laravel, one of the most common stumbling blocks occurs when trying to pre-select an option in a <select> element. The standard HTML syntax for this involves adding the selected attribute to the desired <option>, as seen in your example:

<select name="myselect" id="myselect">
    <option value="1">Item 1</option>
    <option value="2" selected='selected'>Item 2</option>
</select>

Laravel's Blade templating engine, however, does not rely on manually adding attributes to every single option tag. Instead, it leverages the choices helper function to generate the list of options and a separate mechanism to handle the selection state based on your model data. To achieve the desired output, you need to pass two distinct pieces of information to the Blade directive: the array of available choices and the value currently bound to the form field.

Using the choices Helper with Model Data

The most robust approach is to bind the form field directly to a model property or a specific variable using Laravel's old helper, then pass that value into the choices function. The second argument of this function determines which option gets the selected attribute rendered in the final HTML.

Here is how you implement it:

<select name="myselect" id="myselect">
    @foreach($availableItems as $value => $label)
        @if(old('myselect') == $value || request('myselect') == $value)
            <option value="{{ $value }}" selected>{{ $label }}</option>
        @else
            <option value="{{ $value }}">{{ $label }}</option>
        @endif
    @endforeach
</select>

While the loop above demonstrates the logic, Laravel provides a cleaner syntax using the choices helper. This method automatically handles the iteration and attribute assignment:

<select name="myselect" id="myselect">
    @foreach($availableItems as $value => $label)
        <option value="{{ $value }}" {{ old('myselect') == $value ? 'selected' : '' }}>
            {{ $label }}
        </option>
    @endforeach
</select>

Alternatively, if you are using a collection or an array where the key represents the value and the item represents the label, you can use the choices helper directly:

<select name="myselect" id="myselect">
    @foreach($availableItems as $value => $label)
        <option value="{{ $value }}" {{ old('myselect') == $value ? 'selected' : '' }}>
            {{ $label }}
        </option>
    @endforeach
</select>

The choices Helper Syntax

Laravel includes a dedicated helper to simplify this process. You can pass the array of options and the value you want to select as arguments. This is particularly useful when populating dropdowns based on database relations or simple arrays.

<select name="myselect" id="myselect">
    @foreach($availableItems as $value => $label)
        <option value="{{ $value }}" {{ old('myselect') == $value ? 'selected' : '' }}>
            {{ $label }}
        </option>
    @endforeach
</select>

In this implementation, the old() function is crucial. When a form validation fails and the page reloads, Laravel automatically repopulates the input fields with the previously submitted values. The selected attribute must be applied to the option that matches the old input value; otherwise, the user will see an empty or incorrect selection state after a failed submit.

Handling Empty States and Null Values

A common pitfall arises when dealing with nullable fields. If your database column allows nulls and you are editing a record where no item is selected, you must ensure your logic handles null gracefully. The comparison old('myselect') == $value will return false if the old value is null, preventing any option from being selected by default unless one is explicitly marked as such in your data structure.

If you need a "None" or empty option to be selectable, you should prepend an entry with a value of an empty string or a specific placeholder like '--'. The logic remains the same: if the model's attribute matches the placeholder, that option receives the selected attribute.

Conclusion

Laravel abstracts the verbosity of manually adding HTML attributes, but understanding the underlying mechanism is essential for handling edge cases like validation failures and null values. By combining the old() helper with conditional rendering or utilizing the choices helper correctly, you ensure that the rendered HTML matches your intended state, maintaining a seamless user experience.

For further assistance with form helpers or advanced validation rules, you might explore resources provided by LaravelCompany.

Our network