1) Introduction
Enums (enumerations) let you define a fixed set of named values in a type-safe way. Since PHP 8.1 introduced native enums, and Laravel embraces modern PHP, enums are now a clean, expressive way to represent limited sets of states, for example, user roles (admin, user, guest), order statuses (pending, paid, shipped), or payment methods.
Why use enums?
- Type safety – prevents invalid values being used across your app.
- Readability – named cases (e.g., UserStatus::ACTIVE) are clearer than magic strings or integers.
- Centralized logic – put related helper methods (labels, colors, conversions) on the enum itself.
- Compatibility with Laravel features – enums integrate nicely with model casting, validation, and Blade.
Requirements
- PHP 8.1+ (native enum support)
- Laravel 12 (or any recent Laravel that runs on PHP 8.1+)
In this guide we'll:
- Create PHP enums
- Use enums on Eloquent models (casting & storage)
- Validate input against enums
- Show enums in Blade views
- Build a complete UserStatus example (migration, model, controller, form)
2) Creating Enums in Laravel 12
Laravel doesn’t need any extra package to support enums, it fully supports PHP native enums introduced in PHP 8.1.
You can create an enum inside the app/Enums directory (you may need to create this folder manually).
Then create a file named UserStatus.php inside app/Enums:
<?php
namespace App\Enums;
enum UserStatus: string
{
case ACTIVE = 'active';
case INACTIVE = 'inactive';
case SUSPENDED = 'suspended';
}
Example Usage in Code
You can use the enum anywhere in your Laravel app:
use App\Enums\UserStatus;
$userStatus = UserStatus::ACTIVE;
if ($userStatus === UserStatus::ACTIVE) {
echo "User is active.";
}
3) Using Enums in Models
One of the best parts of Laravel 12 is that it natively supports casting model attributes to enums, so you can store enum values in the database and work with them as enum objects in PHP.
Let’s connect our UserStatus enum to the User model.
Step 3.1: Update the Migration
First, make sure your users table has a status column that can store the enum value.
Open your migration file,
(usually database/migrations/xxxx_xx_xx_create_users_table.php) and add:
$table->string('status')->default('active');
then run:
php artisan migrate
Step 3.2: Cast the Attribute to Enum
Open the User model (app/Models/User.php) and add enum casting like this:
<?php
namespace App\Models;
use App\Enums\UserStatus;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $casts = [
'status' => UserStatus::class,
];
}
Step 3.3: Example Usage
Now you can use the enum directly when setting or retrieving model data:
use App\Models\User;
use App\Enums\UserStatus;
// Create new user
$user = User::create([
'name' => 'John Doe',
'email' => 'john@example.com',
'status' => UserStatus::ACTIVE,
]);
// Check status
if ($user->status === UserStatus::ACTIVE) {
echo "User is active!";
}
// Update status
$user->status = UserStatus::SUSPENDED;
$user->save();
4) Validating Enums in Laravel 12
Laravel 12 makes it easy to validate enums using the built-in Enum rule.
Example:
use Illuminate\Validation\Rules\Enum;
use App\Enums\UserStatus;
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'status' => ['required', new Enum(UserStatus::class)],
]);
This ensures that only valid enum values (like active, inactive, or suspended) are accepted.
If an invalid value is passed, Laravel automatically throws a validation error:
The selected status is invalid.
5) Displaying Enums in Blade Views
You can easily display enum values in your Blade templates, just like regular properties.
Example:
<!-- resources/views/users/show.blade.php -->
<h3>User Status: {{ $user->status->value }}</h3>
6) Storing and Retrieving Enums from Database
Laravel automatically casts enums when you define them in your model’s $casts property.
Example:
// app/Models/User.php
use App\Enums\UserStatus;
class User extends Model
{
protected $casts = [
'status' => UserStatus::class,
];
}
Now, when you save or retrieve a user, Laravel automatically converts between the enum and its string value:
// Store enum
$user = new User();
$user->name = 'John Doe';
$user->email = 'john@example.com';
$user->status = UserStatus::ACTIVE;
$user->save();
// Retrieve enum
$user = User::first();
echo $user->status->value; // active
7) Conclusion
Enums in Laravel 12 provide a clean, type-safe way to represent constant values like user roles, statuses, or order types.