Package

Spatie Laravel Enum

Laravel Enum support

Package

spatie/laravel-enum

View on GitHub →

Laravel Support for Spatie Enum

The spatie/laravel-enum package provides robust support for enums within Laravel applications, leveraging the features of the spatie/enum package. It simplifies the integration of strongly typed enums in various aspects of a Laravel project such as model attribute casting, validation, and request data transformation.

Main Features

  • Model Attribute Casting: Facilitates the use of enums as Eloquent model attributes, ensuring type safety and ease of use in data interactions.
  • Validation Rules: Offers a custom validation rule EnumRule to ensure request data matches expected enum values.
  • Request Data Transformation: Allows automatic transformation of request data into enum instances, making controller logic cleaner and more robust.
  • Route Binding: Supports automatic injection of enum instances into route actions, both implicitly and explicitly.
  • Enum Make Command: Provides a convenient Artisan command to generate new enum classes.
  • Faker Provider: Includes a provider to generate random enum values for use in factories, improving the ease of testing models with enum attributes.

Installation

Install the package via composer:

composer require spatie/laravel-enum

Usage

Define enums using the provided base class and utilize them in models, form requests, and routes. For example, to define an enum:

use Spatie\Enum\Laravel\Enum;

final class StatusEnum extends Enum {
    // Define enum methods here
}

Model attribute casting can be defined as:

protected $casts = [
    'status' => StatusEnum::class,
];

For validation, use:

$rules = [
    'status' => new EnumRule(StatusEnum::class),
];

And for transforming request data:

$request->transformEnums($enumCastRules);

Route binding and form requests are also supported, streamlining the handling of enums in route logic and validation processes.

Testing

Run package tests with:

composer test

Additional Resources

For detailed usage examples and advanced configuration options, refer to the package's GitHub repository. Here, you can also find information on contributing to the package, accessing the changelog, and handling security issues.

Spatie Laravel Enum Related Articles

Laravel Enum Package for Generating enum-php Classes image

Laravel Enum Package for Generating enum-php Classes

Read article
Media Library Pro: Vue, React and Blade components to handle uploads (sponsor) image

Media Library Pro: Vue, React and Blade components to handle uploads (sponsor)

Read article
Spatie Shares Their Coding Guidelines as AI Skills image

Spatie Shares Their Coding Guidelines as AI Skills

Read article
Enhanced Enum Processing with Laravel's Default Parameter Support image

Enhanced Enum Processing with Laravel's Default Parameter Support

Read article
Image Optimization With Spatie Laravel Image Optimizer image

Image Optimization With Spatie Laravel Image Optimizer

Read article
Driver-Based Architecture in Spatie's Laravel PDF v2 image

Driver-Based Architecture in Spatie's Laravel PDF v2

Read article

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

Let your users buy domains without leaving your app

If your Laravel app gives users a site, a storefront, or a workspace, they’ll eventually want their own domain. The name.com API lets them search, register, and connect one without leaving your product. It’s a clean REST API, so Laravel’s HTTP client is all you need.

Let your users buy domains without leaving your app

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift