laravel-enso / enums
Enums for Laravel Enso
Requires
- php: ^8.0
- laravel-enso/core: ^9.0
- laravel-enso/helpers: ^9.0
- laravel/framework: ^7.0|^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
- dev-master
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- dev-upgrade/laravel13-core12
- dev-feature/laravel12
- dev-upgrade/enso6
- dev-upgrade/enso6-dr
- dev-upgrade/laravel8
- dev-fixes/stylci
This package is auto-updated.
Last update: 2026-04-22 07:04:44 UTC
README
Description
Enums provides both legacy class-based enumerations and frontend-ready native PHP enum discovery for Laravel Enso.
The package keeps backwards compatibility with Enso's original enum classes while also scanning vendor and application Enums folders for native PHP enums that implement specific contracts. Those enums can then be registered into application state and exposed to the frontend in a normalized shape.
It is used throughout the Enso ecosystem for typed option lists, shared UI state, and enum mapping between backend code and frontend consumers.
::: warning Note Since PHP now provides native enums, we strongly recommend migrating new and existing code to native PHP enums wherever possible.
We still intend to keep supporting the Enso enum registry layer for ecosystem integration and frontend state registration, even as the legacy custom enum implementation is phased out. :::
Installation
Install the package:
composer require laravel-enso/enums
The package auto-registers:
LaravelEnso\Enums\AppServiceProviderLaravelEnso\Enums\EnumServiceProvider
If you want to customize which vendor namespaces are scanned, publish the configuration:
php artisan vendor:publish --tag=enums-config
If you want an application provider for registering legacy enums explicitly, publish the stub:
php artisan vendor:publish --tag=enum-provider
Default configuration:
return [ 'vendors' => ['laravel-enso'], ];
For native frontend enums, place enum classes under your PSR-4 Enums folder. In a standard Laravel application this means app/Enums.
Features
- Provides a base class for legacy class-based enums.
- Supports native PHP enums for frontend/state registration.
- Scans configured vendor packages and the host application for enum classes.
- Registers enum state under a single
enumsstore payload. - Exposes a facade for registering and removing legacy enums.
- Supports optional enum mapping through a dedicated
Mappablecontract. - Includes helper traits for select lists, searching, and random case selection.
Usage
Legacy Class-Based Enums
Extend LaravelEnso\Enums\Services\Enum:
use LaravelEnso\Enums\Services\Enum; class Roles extends Enum { public const Admin = 1; public const Supervisor = 2; public const User = 3; }
Available helpers:
Roles::get(1); Roles::has(2); Roles::keys(); Roles::values(); Roles::select(); Roles::json();
Native PHP Enums For Frontend State
Implement LaravelEnso\Enums\Contracts\Frontend on a backed enum:
use LaravelEnso\Enums\Contracts\Frontend; use LaravelEnso\Enums\Contracts\Mappable; use LaravelEnso\Enums\Traits\Select; enum Status: int implements Frontend, Mappable { use Select; case Draft = 0; case Published = 1; public static function registerBy(): string { return 'statuses'; } public function map(): mixed { return match ($this) { self::Draft => 'Draft', self::Published => 'Published', }; } }
The enum will be discovered from an Enums folder and registered under the key returned by registerBy().
Legacy Enum Registration
Register legacy enums through the facade or EnumServiceProvider:
use LaravelEnso\Enums\Facades\Enums; Enums::register([ 'roles' => \App\Enums\Roles::class, ]);
API
Configuration
config/enums.php
Keys:
vendors
The scanner checks:
vendor/<configured-vendor>/*- the application base path and its PSR-4 root
Service Providers
LaravelEnso\Enums\AppServiceProviderLaravelEnso\Enums\EnumServiceProvider
Responsibilities:
- merge and publish config
- publish the legacy enum provider stub
- register legacy enums listed in the provider's
$registerproperty
Legacy Enum Base Class
LaravelEnso\Enums\Services\Enum
Key methods:
constants(): arrayget($key)has($key): boolkeys()values()json(): stringarray(): arrayall(): arrayobject(): objectcollection()select()localisation(bool $state = true): void
Native Enum Discovery
LaravelEnso\Enums\Services\SourceLaravelEnso\Enums\Services\Enums
Requirements for discovery:
- enum must exist in an
Enumsfolder - enum must implement
LaravelEnso\Enums\Contracts\Frontend
Optional mapping:
- implement
LaravelEnso\Enums\Contracts\Mappable
Legacy Enum Registry
LaravelEnso\Enums\Services\LegacyEnumsLaravelEnso\Enums\Facades\Enums
Key methods:
register($enums)remove($aliases)all(): array
State Provider
LaravelEnso\Enums\State\Enums
Provides merged enum state for frontend consumption under the enums store key.
Traits
LaravelEnso\Enums\Traits\SelectLaravelEnso\Enums\Traits\SearchLaravelEnso\Enums\Traits\Random
Depends On
Required Enso packages:
Framework dependency:
Companion frontend package:
Contributions
are welcome. Pull requests are great, but issues are good too.
Thank you to all the people who already contributed to Enso!