Modern Laravel commonly uses Attribute.
protected function fullName(): Attribute
{
return Attribute::make(
get: fn ($value, $attributes) =>
$attributes['first_name'] . ' ' . $attributes['last_name']
);
}
Then:
$user->full_name;
A mutator transforms data when assigning it.
Casts convert database values into useful PHP types:
protected function casts(): array
{
return [
'is_active' => 'boolean',
'settings' => 'array',
'birth_date' => 'date',
];
}
Then:
$user->settings['theme'];
Scenario
If settings is stored as JSON in MySQL, an array cast means application code doesn't need to manually call json_decode() every time.