{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L1784-L1820","documentation":"Thrown as InvalidCastException by isClassCastable() when the cast type for an attribute is neither a recognized primitive cast (in $primitiveCastTypes) nor a loadable class (class_exists() is false). Eloquent assumes class-based casts must reference an existing class; an unknown string is treated as a typo rather than silently ignored.","triggerScenarios":"Listing a cast like 'jsonb', 'encrypted', or a misspelled/renamed class name in $casts that is not a primitive and does not resolve to an existing class. Triggered when Eloquent evaluates class-castability for the key (e.g. on attribute access that routes through isClassCastable()).","commonSituations":"Misspelling a cast ('datetimes' instead of 'datetime'); referencing a custom caster class that was deleted, not autoloaded, or namespaced incorrectly; using a cast name from an older/newer Laravel version that does not exist here; forgetting to composer dump-autoload after adding a caster class.","solutions":["Correct the cast name to a recognized primitive (datetime, json, encrypted, etc.) or the FQCN of a real caster class.","Verify the caster class exists and is autoloaded (composer dump-autoload, check PSR-4 path).","If using a Castable, reference its class name; the Castable resolves to the real caster.","Remove or comment out the invalid cast entry if no longer needed."],"exampleFix":"// before\nprotected $casts = [\n    'published_at' => 'datetimes', // typo, not a primitive, no such class\n];\n\n// after\nprotected $casts = [\n    'published_at' => 'datetime',\n];","handlingStrategy":"validation","validationCode":"foreach ((function () { $m = new YourModel; $r = new \\ReflectionObject($m); return $r->getProperty('casts')->getValue($m); })() as $attr => $cast) {\n    $castType = strpos($cast, ':') !== false ? substr($cast, 0, strpos($cast, ':')) : $cast;\n    $primitives = ['int','integer','real','float','double','decimal','string','bool','boolean','object','array','json','collection','date','datetime','timestamp','encrypted','encrypted:array','encrypted:collection','encrypted:json','encrypted:object','AsArrayObject','AsCollection','AsEncryptedArrayObject','AsEncryptedCollection'];\n    if (! in_array($castType, $primitives, true) && ! class_exists($castType)) {\n        throw new \\LogicException(\"Unknown cast '{$cast}' for attribute {$attr}\");\n    }\n}","typeGuard":"function castTypeResolves(string $castType): bool\n{\n    return in_array($castType, [\n        'int','integer','real','float','double','decimal:','string','bool','boolean',\n        'object','array','json','collection','date','datetime','timestamp',\n        'encrypted','hashed',\n    ], true) || class_exists($castType) || enum_exists($castType);\n}","tryCatchPattern":null,"preventionTips":["Reference caster classes via their ::class constant so a missing class surfaces at static analysis time.","Run composer dump-autoload after adding a custom caster class.","Add a boot test that iterates a model's $casts and asserts each non-primitive entry resolves to a real class."],"tags":["eloquent","cast","invalid-cast","configuration","typo"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}