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

array
casts()

Returns the casts for this model. Override to declare casts with a method instead of the $casts property.

array
getCasts()

Returns all casts from the $casts property and the casts() method.

CarbonInterface|null
date(string $attribute)

Returns an attribute as a Carbon instance in the application timezone.

CarbonInterface|null
asDateTime(mixed $value, string $type = 'datetime')

Converts a raw value into a Carbon instance according to a cast type.

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.

array
toArray()

Returns the model's public properties with casts applied.

mixed
jsonSerialize()

Used by json_encode().

array
parseCast(string $cast)

Splits "type:format" and validates the type.

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.

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.

Return Value

array

at line 62
array getCasts()

Returns all casts from the $casts property and the casts() method.

Return Value

array

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.

Parameters

string $attribute

The property name, e.g. 'created_at'.

Return Value

CarbonInterface|null

Null when the attribute is unset or empty.

Exceptions

InvalidArgumentException

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.

Parameters

mixed $value

Raw value: string, Unix timestamp, or DateTimeInterface.

string $type

One of the supported cast types.

Return Value

CarbonInterface|null

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.

Parameters

CarbonInterface $date

Return Value

string

at line 135
array toArray()

Returns the model's public properties with casts applied.

Return Value

array

at line 163
mixed jsonSerialize()

Used by json_encode().

Return Value

mixed

at line 174
private array parseCast(string $cast)

Splits "type:format" and validates the type.

Parameters

string $cast

Return Value

array

Exceptions

InvalidArgumentException

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.

Return Value

array