How to use "enum" values in select option in Laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Use enum Values in Select Options in Laravel: A Clean Approach
As a senior developer working with the Laravel ecosystem, we frequently encounter scenarios where database constraints, like ENUM types, need to be translated into user-friendly, interactive form elements. The issue you are facingâconditionally setting the selected attribute within a Blade template using complex @if statementsâis a common symptom of trying to manage presentation logic directly within the view layer rather than handling the data preparation in the controller or model.
This post will guide you through the most robust, clean, and scalable way to handle displaying and pre-selecting options based on your database ENUM values in Laravel Blade.
The Problem with Conditional Selection in Blade
Your current approach using nested @if statements works, but it quickly becomes unwieldy, hard to maintain, and scales poorly if you have more than two or three choices.
<!-- Example of the complex logic you described -->
<select class="form-control" name="contact_way">
@if ($customer_event->contact_way === "email")
<option value="email" selected>Kontakt e-mail</option>
<option value="phone">Kontakt telefoniczny</option>
@else
<option value="email">Kontakt e-mail</option>
<option value="phone" selected>Kontakt telefoniczny</option>
@endif
</select>While this achieves the goal, it tightly couples your presentation logic to specific data points. A better approach is to separate what needs to be displayed (the options) from how they are selected (the initial state).
The Recommended Solution: Iterating Over Data
The most idiomatic Laravel way to handle dynamic lists of options is by preparing an array or a Collection in your Controller and then iterating over it in the Blade file. This separates the data fetching logic from the presentation logic, adhering to the principle of separation of concerns.
Step 1: Prepare the Data in the Controller
Instead of trying to deduce the selected value in the view, let the controller determine exactly which options should be displayed and which one should be marked as selected.
Assume you are fetching a record from your database that has an contact_way of 'email'. You can pass this data directly to the view.
// Example Controller method (e.g., in a CustomerController)
public function showEditForm(Customer $customer)
{
// Fetch all available options from the enum, or just the current record's value
$availableWays = ['email', 'phone'];
$selectedValue = $customer->contact_way;
return view('customer.edit_form', compact('customer', 'availableWays', 'selectedValue'));
}Step 2: Implement the foreach Loop in Blade
Now, use a simple foreach loop to generate all the <option> tags. Inside the loop, you check if the current option's value matches the $selectedValue we passed from the controller. This is far cleaner and much more scalable than nested conditionals.
<select class="form-control" name="contact_way">
@foreach ($availableWays as $way)
{{-- Check if the current loop value ($way) matches the selected value --}}
<option
value="{{ $way }}"
{{ $way === $selectedValue ? 'selected' : '' }}
>
{{ ucwords(str_replace('_', ' ', $way)) }}
</option>
@endforeach
</select>Explanation of the improvement:
- Clarity: The code clearly states: "Loop through all available contact ways and select the one that matches the current record's value."
- Scalability: If you add a third option ('sms'), you only update the
$availableWaysarray in the controller; the Blade logic remains untouched. - Maintainability: This pattern is much easier for other developers (and future you!) to read and debug than deeply nested conditional blocks.
Best Practices for Database ENUMs in Laravel
When working with ENUM types, it's crucial to ensure your Eloquent model is set up correctly. While MySQL handles the storage, Laravel benefits from explicit casting. Ensure your migration uses appropriate column definitions, and if you are using newer Laravel features, leverage Eloquentâs ability to handle these string-based values effectively. For deeper dives into robust data modeling within Laravel, exploring concepts discussed on the official platform, like those found at https://laravelcompany.com, is highly recommended.
Conclusion
Avoid using complex conditional logic inside your Blade files for simple list rendering. Instead, embrace the power of data preparation in your controller and use straightforward foreach loops to build dynamic form elements. By separating data handling from presentation, you write code that is cleaner, more maintainable, and adheres to superior design principles.