All packages
Enums for Laravel
API reference
The Helpers trait adds 17 static methods and 10 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. |
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. |
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 | Str::headline() of the value (or name), looked up in the translator. |
label() | string | Alias of readable(). |
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. |
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.