Maximizing PHP Enums in Laravel and Filament
Laravel Daily////2 min read
Overview of Centralized Enum Logic
PHP enums offer more than just a list of constants; they serve as a single source of truth for application logic. By integrating enums within and , you adhere to the Single Responsibility Principle. Instead of scattering status strings and conditional styling across your views and controllers, you centralize icons, colors, and labels within the enum itself. This architecture ensures that a single change to an enum case automatically propagates throughout your entire UI and backend.
Prerequisites
To follow this guide, you should be familiar with:
- PHP 8.1+ (for native Enum support)
- Laravel Eloquent (casting and scopes)
- Filament TALL stack (Tables, Forms, and Actions)
Key Libraries & Tools
- : The core PHP framework handling data persistence and casting.
- : An admin panel builder that leverages enums for badges and filters.
- : A static analysis tool used to verify enum implementation standards.
Code Walkthrough
1. Defining the Enum with Filament Interfaces
Implementing interfaces like HasColor and HasLabel allows Filament to interpret the enum automatically.
enum OrderStatus: string implements HasColor, HasLabel, HasIcon {
case Pending = 'pending';
case Shipped = 'shipped';
public function getColor(): string {
return match($this) {
self::Pending => 'warning',
self::Shipped => 'success',
};
}
public function isFinal(): bool {
return in_array($this, [self::Shipped]);
}
}
2. Integration with Eloquent and Migrations
Cast the attribute in your Model to enable IDE autocompletion and prevent typos during data entry.
protected $casts = [
'status' => OrderStatus::class,
];
// In Migration
$table->string('status')->default(OrderStatus::Pending->value);
Syntax Notes
- Backed Enums: Always use string-backed enums (e.g.,
enum Name: string) for database compatibility. - Match Expressions: Use
match($this)inside enum methods to return specific metadata based on the current case.
Practical Examples
- Filament Tables: Use
Tables\Columns\TextColumn::make('status')->badge()to instantly render icons and colors defined in the enum. - Conditional Actions: Hide "Approve" buttons by checking
$record->status->isFinal()directly within the action's visibility closure.
Tips & Gotchas
- Avoid String Literals: Never hardcode
'cancelled'in queries; useOrderStatus::Cancelled->valueto prevent silent bugs from typos. - Static Analysis: Use to ensure you aren't using string icons when enums are preferred.

Reuse Enums Across Your Laravel/Filament App (Stop Repeating Yourself)
WatchLaravel Daily // 8:23