Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lolsokje's avatar

Using Laravel's new accessor/mutator syntax for serialization

Laravel's latest release includes a new and improved way of defining accessors and mutators (as explained in this Larabit by Jeffrey). I much prefer this way of defining accessors and mutators, however they don't seem to work with serialization, unless I'm doing something wrong. I happen to use a getFullNameAttribute in my application as well, which I had defined as

public function getFullNameAttribute(): string
{
    return trim("$this->first_name $this->last_name");
}

and provided to my Vue front end by adding it to the $appends array;

protected $appends = [
    'fullName',
];

However, when replacing this accessor with the new syntax as such;

public function fullName(): Attribute
{
    return Attribute::get(fn() => trim("$this->first_name $this->last_name"));
}

I get a Call to undefined method App\Models\Model::getFullNameAttribute() exception. I can't find anything in the original pull request and its discussion nor has the documentation been updated yet, so does anyone here happen to know if this new feature is usable with serialization?

0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Does it work if you use snake case?

protected $appends = [
    'full_name',
]; 
lolsokje's avatar

@Sinnbeck It does indeed, thanks! Much better since PhpStorm has stopped complaining about fullName being an unresolved variable.

Please or to participate in this conversation.