Enum Case Not Found
Fatal error: Uncaught ValueError: "admin" is not a valid backing value for enum "App\Enum\UserRole"Quick Answer
The value you are trying to convert to an enum does not match any of the defined cases. Use tryFrom() instead of from() to handle invalid values gracefully, or verify the value before conversion.
Why This Happens
PHP 8.1 backed enums have a from() method that throws ValueError when the given value does not match any case. This commonly happens with user input, database values, or API data that contains unexpected values. Use tryFrom() to get null instead of an exception.
The Problem
enum UserRole: string {
case User = 'user';
case Admin = 'administrator';
}
$role = UserRole::from('admin'); // 'admin' doesn't match 'administrator'The Fix
enum UserRole: string {
case User = 'user';
case Admin = 'administrator';
}
$role = UserRole::tryFrom('admin'); // Returns null
if ($role === null) {
$role = UserRole::User; // Fallback to default
}Step-by-Step Fix
- 1
Check the enum cases
Look at the enum definition to see the exact backing values. They must match exactly, including case sensitivity.
- 2
Use tryFrom for safe conversion
Replace from() with tryFrom() which returns null instead of throwing an exception for invalid values.
- 3
Validate input before conversion
Check if the value is valid before converting, or handle the null return from tryFrom() with a sensible default.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free