HasCasts
trait HasCasts
Read-time date casting and JSON serialization for models.
Model properties keep the raw values PDO loaded from the database (UTC strings such as "2026-03-15 14:30:00"). Nothing about loading or saving changes. Casting happens only when you ask for it:
$user->date('created_at'); // CarbonInterface in the app timezone $user->toArray(); // casts applied, dates serialized json_encode($user); // same as toArray()
Declare casts with a property or a method (the method wins on conflict):
protected array $casts = [ 'created_at' => 'datetime', 'birthday' => 'date', 'starts_at' => 'datetime:Y-m-d H:i', ];
protected function casts(): array { return ['published_at' => 'immutable_datetime']; }
Supported types:
- datetime, immutable_datetime: a moment in time stored in UTC. Serialized as ISO 8601 UTC, e.g. 2026-03-15T14:30:00Z.
- date, immutable_date: a calendar date with no time or timezone. Serialized as Y-m-d, e.g. 2026-03-15. Append :format to any type to choose the serialized format instead. Custom formats are rendered in the application timezone.
Properties
| static private array | $emptyDates | Values treated as "no date". |
|
| static private array | $dateCastTypes | Cast types this trait understands. |
Methods
Returns the casts for this model. Override to declare casts with a method instead of the $casts property.
Returns all casts from the $casts property and the casts() method.
Returns an attribute as a Carbon instance in the application timezone.
Converts a raw value into a Carbon instance according to a cast type.
Formats a datetime for JSON. Override to change the wire format for every datetime cast on a model that has no explicit :format.
Returns the model's public properties with casts applied.
Used by json_encode().
Splits "type:format" and validates the type.
Returns only public properties, exactly what json_encode() exposed before this trait existed. Called from outside class scope so protected and private properties are excluded.
Details
at line 53
protected array
casts()
Returns the casts for this model. Override to declare casts with a method instead of the $casts property.
at line 62
array
getCasts()
Returns all casts from the $casts property and the casts() method.
at line 77
CarbonInterface|null
date(string $attribute)
Returns an attribute as a Carbon instance in the application timezone.
Works for any attribute holding a date, whether or not it is declared in $casts. Undeclared attributes are treated as datetime.
at line 97
protected CarbonInterface|null
asDateTime(mixed $value, string $type = 'datetime')
Converts a raw value into a Carbon instance according to a cast type.
Datetime strings without a timezone are read as UTC. Plain Y-m-d strings are calendar dates and are never shifted between timezones.
at line 126
protected string
serializeDate(CarbonInterface $date)
Formats a datetime for JSON. Override to change the wire format for every datetime cast on a model that has no explicit :format.
The default, ISO 8601 in UTC with a Z suffix, is parsed the same way by every browser and by Chappy's JavaScript datetime utilities.
at line 135
array
toArray()
Returns the model's public properties with casts applied.
at line 163
mixed
jsonSerialize()
Used by json_encode().
at line 174
private array
parseCast(string $cast)
Splits "type:format" and validates the type.
at line 194
private array
publicAttributes()
Returns only public properties, exactly what json_encode() exposed before this trait existed. Called from outside class scope so protected and private properties are excluded.