NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
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

MethodReturnsPurpose
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()intThe number of cases.
random()staticA 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)staticCase by name; throws EnumException on a miss.
tryFromName(?string $name)?staticCase by name, or null.
fromLabel(string $label)staticCase by readable label; throws EnumException on a miss.
tryFromLabel(?string $label)?staticCase by readable label, or null.
hasName(string $name)boolWhether a case has this name.
hasValue(string|int $value)boolWhether a case has this value — the name on a pure enum (strict).
validationRule()stringA Laravel in: rule from values(), quoting values that break its CSV syntax.

Instance methods

MethodReturnsPurpose
readable()stringStr::headline() of the value (or name), looked up in the translator.
label()stringAlias of readable().
is(self $enum)boolWhether it is the given case.
isNot(self $enum)boolWhether it is not the given case.
isIn(array $enums)boolWhether it is one of the given cases.
isNotIn(array $enums)boolWhether it is none of the given cases.
whenIs(self $enum, Closure $callback, ?Closure $default = null)selfRuns $callback when the condition holds, else the optional $default; returns the case.
whenIsNot(self $enum, Closure $callback, ?Closure $default = null)selfRuns $callback when the condition holds, else the optional $default; returns the case.
whenIsIn(array $enums, Closure $callback, ?Closure $default = null)selfRuns $callback when the condition holds, else the optional $default; returns the case.
whenIsNotIn(array $enums, Closure $callback, ?Closure $default = null)selfRuns $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 crypto

By 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.