{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L1315-L1351","documentation":"getStorableEnumValue() is invoked when persisting an enum-cast attribute. It requires the value be an instance of the enum class declared in $casts. If a different enum (or any non-matching UnitEnum) is assigned, a ValueError is thrown because the value cannot be safely stored for that column.","triggerScenarios":"Assigning $model->status = OtherStatus::Active when 'status' is cast to AppStatus; passing a backed-enum of a different type; assigning a UnitEnum where a BackedEnum is expected and the framework cannot extract a scalar.","commonSituations":"Two similarly-named enums in the codebase; refactor that changed the enum class but left stale data or callers; receiving enum values from an API/form request that maps to the wrong enum; cross-enum assignments in tests.","solutions":["Match the enum class exactly: ensure the assigned value is an instance of the cast-target enum.","Convert via the enum's from()/tryFrom() before assignment: $model->status = AppStatus::from($incoming->value).","Update $casts to the correct enum class if the contract changed."],"exampleFix":"// before\n$model->status = OtherStatus::Active; // status cast to AppStatus\n\n// after\n$model->status = AppStatus::from(OtherStatus::Active->value);","handlingStrategy":"type-guard","validationCode":"$expected = \\App\\Enums\\AppStatus::class;\nif (! $value instanceof $expected) {\n    $value = $expected::from((string) $value); // or tryFrom with fallback\n}\n$model->status = $value;","typeGuard":"function isExpectedEnum(mixed $value, string $enumClass): bool\n{\n    return $value instanceof $enumClass;\n}","tryCatchPattern":"try {\n    $model->status = $value;\n} catch (\\ValueError $e) {\n    if (str_contains($e->getMessage(), 'is not of the expected enum type')) {\n        $model->status = \\App\\Enums\\AppStatus::tryFrom((string) $value) ?? \\App\\Enums\\AppStatus::Default;\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Keep a single source of truth for each enum and reference it via ::class in $casts and callers.","Use Form Request validation with Enum::when rules to convert input to the right enum before assignment.","Assert cross-enum assignments are intentional and convert via from()/tryFrom()."],"tags":["eloquent","cast","enum","type-error"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}