Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Laravel Enum

spatie/laravel-enum image

Laravel Enum stats

Downloads
2.3M
Stars
291
Open Issues
3
Forks
32

View on GitHub →

Laravel Enum support

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 photo

We create open source, digital products and courses for the developer community


Spatie Laravel Enum Related Articles

Create Select Options from Enums, Laravel Models, and more image

Create Select Options from Enums, Laravel Models, and more

Read article
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Harpoon: Next generation time tracking and invoicing logo

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
Acquaint Softtech logo

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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
PhpStorm logo

PhpStorm

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

PhpStorm
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell