{"id":"9664fd90ac3c5e1b","repo":"laravel/framework","slug":"call-to-undefined-cast-casttype-on-column","errorCode":null,"errorMessage":"Call to undefined cast [{$castType}] on column [{$column}] in model [{$class}].","messagePattern":"Call to undefined cast \\[(.+?)\\] on column \\[(.+?)\\] in model \\[(.+?)\\]\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php","lineNumber":1802,"sourceCode":"    protected function isClassCastable($key)\n    {\n        $casts = $this->getCasts();\n\n        if (! array_key_exists($key, $casts)) {\n            return false;\n        }\n\n        $castType = $this->parseCasterClass($casts[$key]);\n\n        if (in_array($castType, static::$primitiveCastTypes)) {\n            return false;\n        }\n\n        if (class_exists($castType)) {\n            return true;\n        }\n\n        throw new InvalidCastException($this->getModel(), $key, $castType);\n    }\n\n    /**\n     * Determine if the given key is cast using an enum.\n     *\n     * @param  string  $key\n     * @return bool\n     */\n    protected function isEnumCastable($key)\n    {\n        $casts = $this->getCasts();\n\n        if (! array_key_exists($key, $casts)) {\n            return false;\n        }\n\n        $castType = $casts[$key];\n","sourceCodeStart":1784,"sourceCodeEnd":1820,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L1784-L1820","documentation":"isClassCastable() resolves the cast string; if it is not a primitive type, not an enum, and the class does not exist, Eloquent throws InvalidCastException naming the model, column, and the bad cast type. This surfaces a clearly wrong $casts entry rather than failing later.","triggerScenarios":"Declaring protected $casts = ['foo' => 'App\\Missing\\Caster'] (class does not exist), or a typo like 'datetimes' instead of 'datetime', or a deleted/renamed custom cast class still referenced.","commonSituations":"Refactor that moved/deleted a cast class without updating $casts; IDE auto-completing a wrong FQN; misspelled primitive cast names; namespaces changed during a package rename.","solutions":["Correct or remove the cast entry in $casts; verify the class string with class_exists().","Fix the typo to a valid primitive (integer, float, decimal:N, datetime, json, boolean, string, etc.) or a real caster class.","Run composer dump-autoload after moving cast classes between namespaces."],"exampleFix":"// before\nprotected $casts = [\n    'price' => 'App\\Casts\\MoneyCster', // typo / missing class\n];\n\n// after\nprotected $casts = [\n    'price' => \\App\\Casts\\MoneyCast::class,\n];","handlingStrategy":"validation","validationCode":"foreach ((new \\ReflectionClass($model))->getDefaultProperties()['casts'] ?? [] as $col => $cast) {\n    if (is_string($cast) && ! in_array($cast, ['int','integer','real','float','double','decimal:','bool','boolean','string','array','json','object','date','datetime','timestamp'])) {\n        $cls = \\Illuminate\\Database\\Eloquent\\Model::parseCastClassName($cast) ?? $cast;\n        if (! class_exists($cls) && ! enum_exists($cls)) {\n            throw new \\RuntimeException(\"Undefined cast '{$cast}' for '{$col}'\");\n        }\n    }\n}","typeGuard":"function castResolves(string $cast): bool\n{\n    $primitives = ['int','integer','real','float','double','bool','boolean','string','array','json','object','date','datetime','timestamp','encrypted','encrypted:array','encrypted:json','encrypted:collection','encrypted:object'];\n    [$type] = explode(':', $cast . ':');\n    if (in_array($type, $primitives)) return true;\n    return class_exists($type) || enum_exists($type);\n}","tryCatchPattern":"try {\n    return $model->{$col};\n} catch (\\Illuminate\\Database\\Eloquent\\InvalidCastException $e) {\n    report('Invalid cast on ' . get_class($model) . '::' . $e->column . ' -> ' . $e->castType);\n    throw $e;\n}","preventionTips":["Reference cast classes with ::class, not string literals.","Run composer dump-autoload after moving cast classes.","Add a startup/console test that iterates every model's $casts and verifies each entry resolves."],"tags":["eloquent","cast","class-not-found","config"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}