Installation
Require the package with Composer:
composer require roundly-consulting/enums-for-laravelThat’s the whole setup. Enums for Laravel is built around a single trait: it ships no service provider, config file, migrations, views or Artisan commands, so there’s nothing to publish, migrate or register. There’s no facade either: the helpers live on your own enums, so there’s no stateful API to route through one. The code lives in a handful of classes:
- RoundlyConsulting\Enums\Helpers — the trait you add to your enums.
- RoundlyConsulting\Enums\DataTransferObjects\EnumOption — the value/label/name DTO returned by options().
- RoundlyConsulting\Enums\DataTransferObjects\EnumPresentation — the value/label/color DTO returned by presentation() and presentations().
- RoundlyConsulting\Enums\Attributes\TranslatedLabels — the attribute that reads labels from a translation group.
- RoundlyConsulting\Enums\Contracts\HasColor — the interface that gives cases a display colour.
- RoundlyConsulting\Enums\Exceptions\EnumException — thrown when fromName() or fromLabel() finds no case, random() meets an enum with no cases, validationRule() can’t represent a value, untranslated() meets an enum without #[TranslatedLabels], or a #[TranslatedLabels] group is invalid.
Generate an enum
Laravel’s own make:enum command scaffolds the enum — pass --string or --int for a backed enum. It lands in app/Enums when that directory exists:
php artisan make:enum NewsCategory --stringThen add the trait, as shown in Quick start.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.