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

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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


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
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
PhpStorm logo

PhpStorm

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

PhpStorm
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
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
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic