Where to Put & How to Handle Enums in Laravel?

Learn how to define enums in Laravel, use native PHP enums or the Spatie package, validate user inputs, cast enums in models, and use them in Blade views.

Where to Put & How to Handle Enums in Laravel?
Define enums in Laravel using native PHP 8.1+ enums or Spatie for richer features, then apply them cleanly across models, validation, Blade templates, and API responses.

1. Native PHP Enums in Laravel (PHP 8.1+)

Laravel supports PHP 8.1+ native enums, letting you cleanly use them in models, validation, database logic, and API responses.

Creating an Enum Class

Start by creating a dedicated enum under App\Enums:
namespace App\Enums; enum SubscriptionPlan: string {     case BASIC = 'basic';     case PRO = 'pro';     case ENTERPRISE = 'enterprise'; }

Using Enums in Models

use App\Enums\SubscriptionPlan; use Illuminate\Database\Eloquent\Model; class Customer extends Model {     protected $casts = [         'plan' => SubscriptionPlan::class,     ]; }

Blade View Example

@if ($customer->plan === \App\Enums\SubscriptionPlan::PRO)     You're on the Pro Plan 🚀 @endif

Validating Enum Values in Requests

use Illuminate\Validation\Rule; use App\Enums\SubscriptionPlan; $request->validate([     'plan' => ['required', Rule::in(array_column(SubscriptionPlan::cases(), 'value'))], ]);

API Response Formatting

public function toArray(): array {     return [         'name' => $this->name,         'plan' => $this->plan->value,     ]; }

2. Custom Enum Classes in Laravel (Before PHP 8.1)

Running Laravel on an older PHP version? You can still simulate enum behavior using constant-based classes, perfect for projects stuck below PHP 8.1.

Creating a Custom Enum Class

Here’s a basic structure using constants and a helper method:
namespace App\Enums; class SubscriptionPlan {     public const BASIC = 'basic';     public const PRO = 'pro';     public const ENTERPRISE = 'enterprise';     public static function all(): array     {         return [             self::BASIC,             self::PRO,             self::ENTERPRISE,         ];     } }

Using Custom Enums in Validation

You can plug this into your request validation like this:
use App\Enums\SubscriptionPlan; use Illuminate\Validation\Rule; $request->validate([     'plan' => ['required', Rule::in(SubscriptionPlan::all())], ]);
📚Also Read: Best Laravel Security Practices For Developers

3. Enum Handling with Spatie Package

Spatie’s Laravel Enum package brings extra tools for cleaner validation, type-safe comparisons, and flexible enum-driven workflows.

Installation

First, install the package using Composer:
composer require spatie/enum

Creating an Enum Class (Spatie)

You can generate your enum manually or extend the base Enum class like this:
namespace App\Enums; use Spatie\Enum\Enum; /**  * @method static self monthly()  * @method static self yearly()  * @method static self lifetime()  */ class BillingCycle extends Enum {}

Form Request Validation

use App\Enums\BillingCycle; use Illuminate\Validation\Rules\Enum; $request->validate([     'cycle' => ['required', new Enum(BillingCycle::class)], ]);

Model Attribute Casting

public function setCycleAttribute($value) {     $this->attributes['cycle'] = BillingCycle::make($value)->value; } public function getCycleAttribute($value) {     return BillingCycle::make($value); }

Blade Template Usage

@if ($subscription->cycle->equals(\App\Enums\BillingCycle::yearly()))     You’re subscribed to the yearly plan 📅 @endif
“ 📊 DID U KNOW? Laravel crossed 80,000+ GitHub stars in 2025, making it one of the top PHP frameworks for scalable apps.

4. Handling Enums in Laravel Migrations and Databases

Store enums cleanly in Laravel using VARCHAR columns or ENUM types, keeping your schema flexible for future changes.

Option 1: Use VARCHAR Columns (Recommended)

Storing enum values as plain strings (e.g., 'admin', 'user', 'guest') offers more flexibility, especially when enum options may evolve in the future.
use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; Schema::create('users', function (Blueprint $table) {     $table->string('role')->default('user'); });

Using Enums in Model Casts

You can easily bind enum classes to your Eloquent model using Laravel’s native casting system (PHP 8.1+ only):
protected $casts = [     'role' => \App\Enums\UserRole::class, ];

Tips

  • Type-Safe Logic: If you're on PHP 8.1 or higher, native enums offer clean, maintainable code with strict type enforcement across your Laravel app.
  • Flexible Database Design: Stick with VARCHAR fields instead of rigid ENUM columns to keep your schema adaptable as your enum options evolve.
  • Reliable Validation: Whether using native enums, custom classes, or Spatie, always validate enum inputs to maintain data integrity and avoid unexpected behavior.

Want to Accelerate your Laravel Solution?

Partner with our Skilled Laravel Developers to build, scale, and achieve Robust Laravel Solutions.