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

SigalZ's avatar

Accessor error

I would like to create an accessor on my user model that will return the first_name and last_name with space between them.

e.g. first_name = Sigal

last_name = Zahavi

$user->fullname should return Sigal Zahavi

From Laravel documentation :

use App\Support\Address;
use Illuminate\Database\Eloquent\Casts\Attribute;
 
/**
 * Interact with the user's address.
 *
 * @return  \Illuminate\Database\Eloquent\Casts\Attribute
 */
protected function address(): Attribute
{
    return Attribute::make(
        get: fn ($value, $attributes) => new Address(
            $attributes['address_line_one'],
            $attributes['address_line_two'],
        ),
    );
}

I don't 100% understand it:

What is App\Support\Address?

What will the function return?

address_line_one and address_line_two of the address model with space in between?

My code:

In the User model:

protected function fullname(): Attribute
{
        return Attribute::make(
            get: fn ($value, $attributes) => new User(
                $attributes['first_name'],
                $attributes['last_name'],
            ),
        );
}

In my view:

<p>{{ Auth::user()->fullname }}</p>

And I get an error:

Illuminate\Database\Eloquent\Model::__construct(): Argument #1 ($attributes) must be of type array, string given, called in C:\wamp64\www\mysite\app\Models\User.php on line 163

I also tried, as a test, to do something on the UserAddr model:

protected function address(): Attribute
{
    return Attribute::make(
        get: fn ($value, $attributes) => new UserAddr(
            $attributes['addr_line_1'],
            $attributes['addr_line_2'],
        ),
    );
}

Then on a controller:

$addr = UserAddr::find(4);
dd($addr->address);

Getting the same error.

I don't understand the documentation.

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

Yeah I find it a bit hard to understand as well :) But try this

protected function fullname(): Attribute
    {
        return Attribute::make(
            get: fn($value, $attributes) => $attributes['first_name'] . ' ' . $attributes['last_name'],
        );
    }
1 like
SigalZ's avatar

@Sinnbeck Thank you again!!! That works. I'm happy to hear I'm not the only one that does not understand the documentation. Very frustrating.

I wish it was written from A to Z, where do you put the code, what do lines in the code mean and what is the result of the code.

Yet, you manage to find the solution and I don't. Can I adopt you? ;)

Sinnbeck's avatar

@SigalZ Haha well I have just tried debugging these things myself waaaaay to many times, so I often have an idea of what was meant, even when the docs arent completely clear. And sadly I am probably to old for adoption :D

sos99's avatar

Hi Sigal

Yet, you manage to find the solution and I don't. Can I adopt you? ;)

Please adopt the documentation! :)

Please or to participate in this conversation.