laravel/framework · error · InvalidCastException
Call to undefined cast
Error message
Call to undefined cast [{$castType}] on column [{$column}] in model [{$class}]. What it means
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.
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.
Example fix
// before
protected $casts = [
'published_at' => 'datetimes', // typo, not a primitive, no such class
];
// after
protected $casts = [
'published_at' => 'datetime',
]; Defensive patterns
Strategy: validation
Validate before calling
foreach ((function () { $m = new YourModel; $r = new \ReflectionObject($m); return $r->getProperty('casts')->getValue($m); })() as $attr => $cast) {
$castType = strpos($cast, ':') !== false ? substr($cast, 0, strpos($cast, ':')) : $cast;
$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'];
if (! in_array($castType, $primitives, true) && ! class_exists($castType)) {
throw new \LogicException("Unknown cast '{$cast}' for attribute {$attr}");
}
} Type guard
function castTypeResolves(string $castType): bool
{
return in_array($castType, [
'int','integer','real','float','double','decimal:','string','bool','boolean',
'object','array','json','collection','date','datetime','timestamp',
'encrypted','hashed',
], true) || class_exists($castType) || enum_exists($castType);
} Prevention
- 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.
When it happens
Trigger: 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()).
Common situations: 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.
Related errors
- Could not verify the hashed value's configuration.
- The cast object for the
- The provided class must extend…
- The provided class must extend…
- Expected class to implement guessResourceName method. Make…
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/9664fd90ac3c5e1b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:1802
protected function isClassCastable($key)
{
$casts = $this->getCasts();
if (! array_key_exists($key, $casts)) {
return false;
}
$castType = $this->parseCasterClass($casts[$key]);
if (in_array($castType, static::$primitiveCastTypes)) {
return false;
}
if (class_exists($castType)) {
return true;
}
throw new InvalidCastException($this->getModel(), $key, $castType);
}
/**
* Determine if the given key is cast using an enum.
*
* @param string $key
* @return bool
*/
protected function isEnumCastable($key)
{
$casts = $this->getCasts();
if (! array_key_exists($key, $casts)) {
return false;
}
$castType = $casts[$key];
View on GitHub (pinned to e0f6eb3518)