{"record":{"id":"e88dfbd7ac5aa9a8","repo":"laravel/framework","slug":"value-s-is-not-of-the-expected-enum-type-s","errorCode":null,"errorMessage":"Value [%s] is not of the expected enum type [%s].","messagePattern":"Value \\[(.+?)\\] is not of the expected enum type \\[(.+?)\\]\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php","lineNumber":1333,"sourceCode":"    {\n        return is_subclass_of($enumClass, BackedEnum::class)\n            ? $enumClass::from($value)\n            : constant($enumClass.'::'.$value);\n    }\n\n    /**\n     * Get the storable value from the given enum.\n     *\n     * @param  string  $expectedEnum\n     * @param  \\UnitEnum  $value\n     * @return string|int\n     *\n     * @throws \\ValueError\n     */\n    protected function getStorableEnumValue($expectedEnum, $value)\n    {\n        if (! $value instanceof $expectedEnum) {\n            throw new ValueError(sprintf('Value [%s] is not of the expected enum type [%s].', var_export($value, true), $expectedEnum));\n        }\n\n        return enum_value($value);\n    }\n\n    /**\n     * Get an array attribute with the given key and value set.\n     *\n     * @param  string  $path\n     * @param  string  $key\n     * @param  mixed  $value\n     * @return array\n     */\n    protected function getArrayAttributeWithValue($path, $key, $value)\n    {\n        return tap($this->getArrayAttributeByKey($key), function (&$array) use ($path, $value) {\n            Arr::set($array, str_replace('->', '.', $path), $value);\n        });","sourceCodeStart":1315,"sourceCodeEnd":1351,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L1315-L1351","documentation":"Thrown by getStorableEnumValue() as a ValueError when a value being stored for an enum-cast attribute is not an instance of the expected enum class. Eloquent casts enum attributes by checking instanceof against the configured enum; on mismatch it raises ValueError with the var_export of the value and the expected enum FQN.","triggerScenarios":"Assigning a wrong-enum value to an enum-cast attribute (e.g. casting to App\\Enums\\Status but assigning a UserStatus case), or assigning a scalar to a typed enum attribute that expects a backed-enum case. Triggered on write/save when the cast serializes the value.","commonSituations":"Two enums with similar names (Status vs UserStatus) and passing the wrong one; assigning a string/int to an attribute cast to an enum instead of the enum case; a migration that changed which enum backs a column but old code still passes the previous enum.","solutions":["Pass an instance of the exact enum the cast expects (e.g. Status::Active, not UserStatus::Active).","Update the cast to reference the correct enum class string.","When accepting user input, convert the raw value to the enum case via Status::from($input) before assignment."],"exampleFix":"// before\nprotected $casts = ['status' => App\\Enums\\Status::class];\n$user->status = App\\Enums\\UserStatus::Active; // ValueError\n\n// after\n$user->status = App\\Enums\\Status::Active;","handlingStrategy":"type-guard","validationCode":"$expectedEnum = App\\Enums\\Status::class;\nif ($value instanceof \\BackedEnum || $value instanceof \\UnitEnum) {\n    if (! $value instanceof $expectedEnum) {\n        throw new \\UnexpectedValueException('Expected ' . $expectedEnum . ', got ' . get_class($value));\n    }\n}\n$model->status = $value;","typeGuard":"function isExpectedEnum(\\UnitEnum $value, string $expectedEnum): bool\n{\n    return $value instanceof $expectedEnum;\n}","tryCatchPattern":"try {\n    $model->status = $input;\n} catch (\\ValueError $e) {\n    report($e);\n    $model->status = App\\Enums\\Status::default();\n}","preventionTips":["Always pass enum *cases*, not scalars, to enum-cast attributes.","Convert raw user input with Status::from($input) (or tryFrom) before assignment.","Keep a single source of truth for each domain enum to avoid passing a same-named sibling enum."],"tags":["eloquent","cast","enum","type-mismatch"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}