roundly-consulting / enums-for-laravel
Convenient helper methods for PHP enums in Laravel applications
Package info
github.com/roundly-consulting/enums-for-laravel
pkg:composer/roundly-consulting/enums-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/container: ^12.0|^13.0
- illuminate/contracts: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/phpstan-deprecation-rules: ^2.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Enums for Laravel
Helper methods for PHP enums in Laravel. Drop the Helpers trait into an enum to get translated
labels, names/values/labels lists, select-ready options, case lookups, a validation rule, random
selection, equality checks and conditional callbacks — for backed and pure enums alike.
Installation
Requires PHP 8.4, Laravel 12 or 13.
composer require roundly-consulting/enums-for-laravel
Usage
Add the Helpers trait to any enum:
use RoundlyConsulting\Enums\Helpers; enum NewsCategory: string { use Helpers; case HotNews = 'hot-news'; case RegularNews = 'regular-news'; case PrivateNews = 'private-news'; }
Then use it for labels, selects, validation and lookups:
NewsCategory::HotNews->label(); // "Hot News", translated through lang/*.json NewsCategory::values()->all(); // ['hot-news', 'regular-news', 'private-news'] NewsCategory::toArray(); // ['hot-news' => 'Hot News', …] for a <select> NewsCategory::options(); // EnumOption DTOs: value, label, name $request->validate([ 'category' => ['required', NewsCategory::validationRule()], // 'in:hot-news,regular-news,private-news' ]); NewsCategory::fromLabel('Hot News'); // NewsCategory::HotNews NewsCategory::tryFromName('Missing'); // null NewsCategory::HotNews->isIn([NewsCategory::HotNews, NewsCategory::RegularNews]); // true
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/enums-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). Please see LICENSE for more information.