How to Work with Enums Easily in Laravel
Yasser Elgammal

Yasser Elgammal @yasserelgammal

About: Software engineer @ AAIT

Location:
Egypt, Damietta
Joined:
Feb 18, 2019

How to Work with Enums Easily in Laravel

Publish Date: Jun 16
3 1

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
Enter fullscreen mode Exit fullscreen mode

(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';
}
Enter fullscreen mode Exit fullscreen mode

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,
    ];
}
Enter fullscreen mode Exit fullscreen mode

Now, whenever you access $ticket->status, it returns an a TicketStatus instance.

$ticket = Ticket::first();
if ($ticket->status === TicketStatus::Open) {
    // handle open ticket
}
Enter fullscreen mode Exit fullscreen mode

Enum Helper Methods

Get all cases:

TicketStatus::cases(); // returns an array of all enum cases
Enter fullscreen mode Exit fullscreen mode

Get all values:

You can wrap this in a static method for reusability:

public static function values(): array
{
    return array_column(self::cases(), 'value');
}
Enter fullscreen mode Exit fullscreen mode

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)],
]);
Enter fullscreen mode Exit fullscreen mode

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' => 'مغلق',
    ],
];
Enter fullscreen mode Exit fullscreen mode

Then create a method on your enum:

public function label(): string
{
    return __("enums.ticket_status.{$this->value}");
}
Enter fullscreen mode Exit fullscreen mode

Now you can do:

$ticket->status->label(); // قيد المعالجة
Enter fullscreen mode Exit fullscreen mode

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.

Comments 1 total

  • david duymelinck
    david duymelinckJun 16, 2025

    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.

    
    enum TicketStatus: string
    {
        case Open = 'open';
        case InProgress = 'in_progress';
        case Resolved = 'resolved';
        case Closed = 'closed';
    
        public function getArabic(): string
        {
              return match($this) {
                 TicketStatus::Open => 'مفتوح',
                 TicketStatus::InProgress' => 'قيد المعالجة',
                 TicketStatus::Resolved => 'تم الحل',
                 TicketStatus::Closed => 'مغلق',
                 default: $this->value
              };
         }
    }
    
    Enter fullscreen mode Exit fullscreen mode
Add comment