How to use improved Eloquent accessors and mutators in Laravel 9.x
Categorized as Laravel
Hello friends! As you may know now, since the version 9 of Laravel you can use the new way to define Eloquent accessors and mutators
// Before
public function getNameAttribute($value)
{
return strtoupper($value);
}
public function setNameAttribute($value)
{
$this->attributes['name'] = $value;
}
// After, in Laravel 9.x, with improved Eloquent accessors & mutators
use Illuminate\Database\Eloquent\Casts\Attribute;
public function (): Attribute
{
return new Attribute(
get: fn ($value) => strtoupper($value),
set: fn ($value) => $value,
);
}