All packages
Enums for Laravel
Exceptions
fromName() and fromLabel() throw RoundlyConsulting\Enums\Exceptions\EnumException when no case matches. It extends PHP’s InvalidArgumentException, so either catch works:
use RoundlyConsulting\Enums\Exceptions\EnumException;
try {
NewsCategory::fromName('Breaking');
} catch (EnumException $e) {
$e->getMessage();
// "No case named [Breaking] exists on enum [App\Enums\NewsCategory]."
}
try {
NewsCategory::fromLabel('Breaking News');
} catch (InvalidArgumentException $e) {
// EnumException extends InvalidArgumentException, so this catches it too
}| Named constructor | Thrown by | Message |
|---|---|---|
EnumException::nameNotFound(string $enum, string $name) | fromName() | No case named [{name}] exists on enum [{enum}]. |
EnumException::labelNotFound(string $enum, string $label) | fromLabel() | No case with label [{label}] exists on enum [{enum}]. |
EnumException is deliberately not final, so you can extend it. For user input, prefer the tryFrom* variants plus validation, and keep from* for trusted data, where a miss is a bug worth surfacing.
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.