Enums in PHP 8.1+ bring structure and clarity to your code, especially when dealing with fixed sets of values like user roles, statuses, or payment types. Laravel Handles Working With Enums Smoothly.
In this article, we’ll explore:
- What Are Enums?
- Creating Enums in Laravel
- Using Enums in Eloquent Models
- Casting Enums
- Enum Helper Methods
- Validation with Enums
- Localization of Enums
What are Enums?
Enums (short for “enumerations”) allow you to define a set of named values. Think of them as a cleaner alternative to using constants or strings directly in your code.
Creating Enums in Laravel
To create an enum in Laravel, use this command:
php artisan make:enum TicketStatus
(Note: This command is built-in by default in Laravel 11)
// app/Enums/TicketStatus.php
namespace App\Enums;
enum TicketStatus: string
{
case Open = 'open';
case InProgress = 'in_progress';
case Resolved = 'resolved';
case Closed = 'closed';
}
Using Enums in Eloquent Models
You can use enums as attribute casts to automatically convert database values into enum instances.
use App\Enums\TicketStatus;
class Ticket extends Model
{
protected $casts = [
'status' => TicketStatus::class,
];
}
Now, whenever you access $ticket->status
, it returns an a TicketStatus instance.
$ticket = Ticket::first();
if ($ticket->status === TicketStatus::Open) {
// handle open ticket
}
Enum Helper Methods
Get all cases:
TicketStatus::cases(); // returns an array of all enum cases
Get all values:
You can wrap this in a static method for reusability:
public static function values(): array
{
return array_column(self::cases(), 'value');
}
Validation with Enums
Use the Enum rule provided by Laravel for form request validation:
use App\Enums\TicketStatus;
use Illuminate\Validation\Rules\Enum;
$request->validate([
'status' => ['required', new Enum(TicketStatus::class)],
]);
Localizing Enum Values
Let’s say you want to display enum values in Arabic or another language. Add a translation file:
// lang/ar/enums.php
return [
'ticket_status' => [
'open' => 'مفتوح',
'in_progress' => 'قيد المعالجة',
'resolved' => 'تم الحل',
'closed' => 'مغلق',
],
];
Then create a method on your enum:
public function label(): string
{
return __("enums.ticket_status.{$this->value}");
}
Now you can do:
$ticket->status->label(); // قيد المعالجة
Summary:
Working with Enums in Laravel helps you write cleaner, safer, and more expressive code.
Enums can significantly enhance the maintainability of your Laravel applications. So start using them today.
The
make:enum
command makes backed enums. The benefit of a backed enum is that it implements the BackeEnum interface.It is not always the best solution, a basic enum might seem useless but the separation of the definition and the value makes it very powerful.
Instead of using a translation file, I would store the translations in the enum. The reason is that is that the values are not going to change, because enums are basically constants as you mentioned. And then it has no ties to a framework.