Laravel Magazine
Attribute::make(): Replacing Accessors and Mutators with One Clean API

Attribute::make(): Replacing Accessors and Mutators with One Clean API

Eric Van Johnson ·

If you've been writing Laravel for a while, your models probably have a scattering of methods like this:

public function getFirstNameAttribute($value)
{
    return ucfirst($value);
}

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

Nothing wrong with it exactly, except that the get and set logic for first_name lives in two different places in the file, named after two different conventions you have to remember, and there's no good way to see "everything this model does to this one field" without hunting through the whole class.

One field, one method

The Attribute class, available via Illuminate\Database\Eloquent\Casts\Attribute, collapses both directions into a single method:

use Illuminate\Database\Eloquent\Casts\Attribute;

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

Same behavior, same field, but now it's one method you can find, read top to bottom, and reason about. The method name (firstName) maps to the first_name column automatically, following Eloquent's usual snake/camel case convention.

Read-only or write-only attributes

You don't have to define both directions. Skip set for a computed, read-only field:

protected function fullName(): Attribute
{
    return Attribute::make(
        get: fn (mixed $value, array $attributes) => "{$attributes['first_name']} {$attributes['last_name']}",
    );
}

The second argument to get gives you the raw attributes array, which is how you build computed values from multiple columns without triggering extra queries or recursive accessor calls.

Caching expensive computed attributes

If a computed attribute does real work — formatting, an external lookup, whatever — chain ->shouldCache() so it only runs once per request per model instance:

protected function formattedAddress(): Attribute
{
    return Attribute::make(
        get: fn () => $this->geocodeAndFormat(),
    )->shouldCache();
}

Without shouldCache(), accessing $user->formatted_address five times in a Blade view runs the accessor five times. With it, the first access computes and caches the result on the model instance for the rest of the request.

Casting objects, not just scalars

Attribute::make() composes well with value objects. Return anything from get, including a typed object:

protected function money(): Attribute
{
    return Attribute::make(
        get: fn ($value) => new Money($value, $this->currency),
        set: fn (Money $value) => $value->toMinorUnits(),
    );
}

If you're already using dedicated cast classes (implements CastsAttributes) for something reusable across multiple models, keep using those — Attribute::make() is for the common case of "one field, one model, get and set logic that doesn't need to be shared." For everything else, it's a straightforward readability upgrade over the old paired-method convention, and it's been the recommended approach since Laravel 9.

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.