// Laravel 9+ (Attribute casting)
class User extends Model {
protected function firstName(): Attribute {
return Attribute::make(
get: fn(string $value) => ucfirst($value), // accessor
set: fn(string $value) => strtolower($value), // mutator
);
}
// Виртуальный атрибут
protected function fullName(): Attribute {
return Attribute::make(
get: fn() => "{$this->first_name} {$this->last_name}",
);
}
// Casts (встроенные)
protected $casts = [
'options' => 'array',
'birthday' => 'date',
'is_admin' => 'boolean',
'status' => StatusEnum::class,
];
}