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 Laravel and Filament, 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 * Laravel: The core PHP framework handling data persistence and casting. * Filament: An admin panel builder that leverages enums for badges and filters. * FilaCheck: 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. ```php 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. ```php 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; use `OrderStatus::Cancelled->value` to prevent silent bugs from typos. * **Static Analysis**: Use FilaCheck to ensure you aren't using string icons when Heroicons enums are preferred.
Heroicons
Products
- Mar 5, 2026
- Sep 3, 2024