Storing money as integer cents or a status as a string means every read needs converting, and the accessor pattern spreads that conversion across the model until nobody is sure which form a property is in.
protected $casts = [
'options' => 'array',
'placed_at' => 'datetime',
'active' => 'boolean',
];
// for a type the framework does not know, an accessor pair
public function getTotalAttribute($value)
{
return new Money($value, $this->currency);
}
public function setTotalAttribute(Money $total)
{
$this->attributes['total'] = $total->cents();
}
The pair has to be written together or the model reads a Money and writes an integer, which produces a bug that only appears on the second save. The array cast silently swallows malformed JSON and returns null rather than throwing, so a column written by something other than the application needs its own handling. A dedicated cast class is not available yet — that arrives with 7.x — so the accessor pair is the pattern for now.