Laravel Enum

bensampo/laravel-enum image

Laravel Enum stats

Downloads
8M
Stars
1,909
Open Issues
5
Forks
158

View on GitHub →

Simple, extensible and powerful enumeration implementation for Laravel.

Laravel Enum Package Summary

Laravel Enum simplifies the use of enumerations in Laravel applications. Authored by Ben Sampson, it enhances the enum utility with a variety of powerful features tailored for the Laravel framework.

Key Features:

  • Simple Enum Classes: Define enums with key-value pairs as class constants.
  • Comprehensive Methods: Includes a full suite of methods to manipulate and query enum instances.
  • Instantiation and Safety: Enums can be instantiated safely, ensuring values are always valid.
  • Flagged/Bitwise Enums: Support for enums that can hold multiple values simultaneously.
  • Attribute Casting: Automatically cast model attributes to enum instances.
  • Validation Rules: Custom validation rules to ensure input adheres to defined enums.
  • Localization: Support for localizing enum descriptions.
  • Artisan Support: Generate enums via Artisan commands and more.
  • Extendability: Can be extended using Laravel's native macro capabilities.

Installation:

Laravel Enum requires PHP 8 and Laravel 9 or 10. Install it using Composer:

composer require bensampo/laravel-enum

Usage:

Create enums using Artisan:

php artisan make:enum UserType

Define enum values directly in the class:

final class UserType extends Enum {
const Administrator = 0;
const Moderator = 1;
const Subscriber = 2;
}

Enums can be instantiated in various ways, including by key, value, or even using static methods:

$admin = UserType::Administrator();

Advanced Features:

  • Bitwise Enums: Useful for sets of permissions, where multiple enum values can be relevant simultaneously.
  • PHPStan and Laravel Nova Integration: Supports static analysis and integration with Laravel Nova.
  • Migration to Native PHP Enums: Guidance on migrating to native PHP enums (PHP 8.1+).

Migrations and Model Integration:

Easily define how enums interact with your database and Eloquent models, including custom casts for model attributes and straightforward migration recommendations.

Localization and Customization:

Localize enum descriptions and customize response outputs easily, enhancing readability and internationalization.

Developer Tools:

Includes a range of Artisan commands to streamline tasks like annotation, conversion to native enums, and more.

This package is ideal for developers looking to utilize enums in a Laravel project, providing robust tools and methods to manage enum values effectively.

BenSampo photo

Full-stack web developer.

Cube

Laravel Newsletter

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


Bensampo Laravel Enum Related Articles

Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance image

Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance

Read article
The artisan dev Command in Laravel 13.16.0 image

The artisan dev Command in Laravel 13.16.0

Read article
Toolkit: Reusable AI Tools for the Laravel AI SDK image

Toolkit: Reusable AI Tools for the Laravel AI SDK

Read article
Typed Translation Accessors in Laravel 13.15.0 image

Typed Translation Accessors in Laravel 13.15.0

Read article
In-Memory Eloquent Models with Truffle image

In-Memory Eloquent Models with Truffle

Read article
Detect and Resolve Laravel Schema Drift with MigrAlign image

Detect and Resolve Laravel Schema Drift with MigrAlign

Read article
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
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
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
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup