Getting selected option on edit form in laravel

Getting selected option on edit form in laravel

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

Stefan Bogdanescu

Founder & Senior Architect

Populating Select Options in Laravel Edit Forms

When building an edit form in Laravel, ensuring that dropdown fields display the existing data correctly is a common requirement. You want to present the current state of an order—such as the selected user, quantity, or status—and allow the developer to modify it if necessary. If you simply render the input fields without checking their current values, your forms will always default to the first option in the list, which can be confusing for users trying to edit records.

The core mechanism for solving this lies in how you bind the model's attributes to your HTML inputs and how you iterate over your available options. In standard Laravel Blade templates, the Form::model helper automatically handles text fields by injecting the current value from the model into the value attribute. However, <select> elements require a slightly different approach because they generate multiple <option> tags based on a collection.

Binding Values to Text Inputs

For static fields like the Order ID or Invoice Number in your example, you are correctly using the value attribute directly:

<input class="form-control" type="text" name="" value="{{ $order->id }}" readonly>

This works because there is only one input tag. The server-side model $order holds the current state, and Blade interpolates that value directly into the DOM. When you submit a PUT request, Laravel captures this value, updates the database, and persists the change.

Handling Select Elements with Collections

The real challenge appears when iterating over options using @foreach. Your current code loops through $users to generate <option> tags:

<select class="form-control" id="type" name="user_id">
    @foreach($users as $user)
        <option value="{{ $user->id }}">{{ $user->name }}</option>
    @endforeach
</select>

This loop does not know that $order belongs to a specific user. To select the correct option, you must compare the ID of the current user in the database with the ID being iterated over. You can achieve this by adding a selected attribute conditionally:

<select class="form-control" id="type" name="user_id">
    @foreach($users as $user)
        <option value="{{ $user->id }}" 
                {{ $order->user_id == $user->id ? 'selected' : '' }}>
            {{ $user->name }}
        </option>
    @endforeach
</select>

The ternary operator checks if the $order model's user_id matches the $user->id. If they match, the selected attribute is injected into the HTML string. The browser then renders that specific option as the default choice. This pattern applies equally to status and quantity fields.

Managing Static Value Lists

For fields like Quantity or Status, where you are hardcoding the options rather than pulling them from a database query, the logic remains similar but relies on value matching instead of ID matching. In your example, the quantity is hardcoded as 1, 2, 3, etc., while the order model likely stores this as an integer:

<select class="form-control" id="type" name="quantity">
    <option value="" selected>Choose a quantity</option>
    <option value="1" {{ $order->quantity == 1 ? 'selected' : '' }}>1</option>
    <option value="2" {{ $order->quantity == 2 ? 'selected' : '' }}>2</option>
    <option value="3" {{ $order->quantity == 3 ? 'selected' : '' }}>3</option>
    <option value="4" {{ $order->quantity == 4 ? 'selected' : '' }}>4</option>
    <option value="5" {{ $order->quantity == 5 ? 'selected' : '' }}>5</option>
</select>

You must ensure that the data types match exactly. If $order->quantity comes from the database as a string (e.g., "1"), but your option values are integers (1), the comparison might fail depending on PHP's version and strictness settings, though loose comparison usually handles this gracefully in Blade. For robustness, it is often safer to explicitly cast one side or ensure the database column is defined as unsignedTinyInteger.

Handling Textarea Fields

Your note field uses a <textarea>, which behaves like a standard text input. Laravel's Form::model helper already handles this automatically if you bind the model correctly:

<textarea name="note" class="form-control" rows="8">
    {{ old('note', $order->note) }}
</textarea>

Using old() is a best practice here. If validation fails and the form re-renders, old() preserves the input the user typed, preventing it from being lost even if the model hasn't been updated yet. However, on an initial successful load of the edit page, $order->note will contain the saved value from the database.

Potential Pitfalls and Validation

A common mistake occurs when the user_id field is optional in your database but mandatory in your form logic. If $order->user_id is null (perhaps for a draft order), the condition $order->user_id == $user->id will evaluate to false for all users, leaving no option selected. In this case, you might want to default to the first valid user or explicitly handle the null state in your view logic.

Additionally, ensure that the @foreach loop actually receives a populated collection. If $users is empty, the dropdown will render with no options at all, breaking the form submission. Always verify that your controller passes valid data to the view before rendering the select element.

By implementing these conditional checks within your Blade loops, you ensure that every edit form accurately reflects the current state of the record, providing a seamless experience for users managing order data.

Our network