Shift with red gradient lettering
Loading navigation...

Adopt attribute accessors/mutators

Premium
Laravel
Experimental

Starting with Laravel 8, you may define an accessor and mutator using a single, non-prefixed method which returns an Illuminate\Database\Eloquent\Casts\Attribute.

Before

public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
 
public function setFirstNameAttribute($value)
{
$this->attributes['first_name'] = strtolower($value);
}

After

protected function firstName(): Attribute
{
return new Attribute(
get: fn ($value) => ucfirst($value),
set: fn ($value) => strtolower($value),
);
}

← Back to the Workbench Tasks