All packages
Enums for Laravel
API reference
The Helpers trait adds 19 static methods and 11 instance methods. Methods declared on your enum win over the trait’s, so any of them can be overridden — and PHP’s native cases(), from() and tryFrom() stay untouched.
Static methods
| Method | Returns | Purpose |
|---|---|---|
storable() | Collection<int, string|int> | Backed values (case names on a pure enum) in declaration order. |
values() | Collection<int, string|int> | Same as storable(). |
names() | Collection<int, string> | Case names in declaration order. |
labels() | Collection<int, string> | readable() of every case. |
collect() | Collection<int, self> | Every case as an enum instance. |
count() | int | The number of cases. |
random() | static | A random case; throws EnumException on an enum with no cases. |
toOptions() | Collection<array-key, string> | value => label map for selects. |
toArray() | array<array-key, string> | toOptions() as a plain array. |
options() | Collection<int, EnumOption> | {value, label, name} DTOs for JS selects. |
presentations() | Collection<int, EnumPresentation> | {value, label, color} DTOs for badges, in declaration order. |
untranslated(?string $locale = null) | Collection<int, static> | Cases of a #[TranslatedLabels] enum with no line in exactly that locale; throws EnumException without the attribute. |
fromName(string $name) | static | Case by name; throws EnumException on a miss. |
tryFromName(?string $name) | ?static | Case by name, or null. |
fromLabel(string $label) | static | Case by readable label; throws EnumException on a miss. |
tryFromLabel(?string $label) | ?static | Case by readable label, or null. |
hasName(string $name) | bool | Whether a case has this name. |
hasValue(string|int $value) | bool | Whether a case has this value — the name on a pure enum (strict). |
validationRule() | string | A Laravel in: rule from values(), quoting values that break its CSV syntax. |
Instance methods
| Method | Returns | Purpose |
|---|---|---|
readable() | string | The #[TranslatedLabels] line if there is one, else Str::headline() of the value (or name; an int as-is), looked up in the translator. |
label() | string | Alias of readable() — override readable(), not this. |
presentation() | EnumPresentation | The case as {value, label, color}. |
is(self $enum) | bool | Whether it is the given case. |
isNot(self $enum) | bool | Whether it is not the given case. |
isIn(array $enums) | bool | Whether it is one of the given cases. |
isNotIn(array $enums) | bool | Whether it is none of the given cases. |
whenIs(self $enum, Closure $callback, ?Closure $default = null) | self | Runs $callback when the condition holds, else the optional $default; returns the case. |
whenIsNot(self $enum, Closure $callback, ?Closure $default = null) | self | Runs $callback when the condition holds, else the optional $default; returns the case. |
whenIsIn(array $enums, Closure $callback, ?Closure $default = null) | self | Runs $callback when the condition holds, else the optional $default; returns the case. |
whenIsNotIn(array $enums, Closure $callback, ?Closure $default = null) | self | Runs $callback when the condition holds, else the optional $default; returns the case. |
Attribute, contract and DTOs
- #[TranslatedLabels(?string $group = null)] — RoundlyConsulting\Enums\Attributes\TranslatedLabels; see Labels from translation files.
- HasColor — RoundlyConsulting\Enums\Contracts\HasColor, one method: color(): string; see Badge colours and presentation.
- EnumOption — value, label, name and toArray(); returned by options().
- EnumPresentation — value, label, color, toArray() and jsonSerialize(); returned by presentation() and presentations().
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.