0

I have tried multiple ways to call below method in the model

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

What I tried:

User::select('first_name', 'last_name')
        ->with([
            'fullName' => function($query){
                $query->select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"));
            }
        ])
        ->where('user_id', $id)
        ->where('is_deleted', 0)
        ->first();

Another way I tried:

User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"))
        ->with([
            'fullName' => function($query){
                $query->select('firstname', 'lastname');
            }
        ])
        ->where('user_id', $id)
        ->where('is_deleted', 0)
        ->first();

But nothing is calling get<>Attribute name.

1
  • Why the mixture of underscores? firstname and first_name - which is it? Commented Jun 1, 2021 at 9:43

5 Answers 5

2

An accessor is not a relation. Try

$user = User::where('user_id', $id)->first();


dd($user->full_name);
Sign up to request clarification or add additional context in comments.

Comments

2

These are magic methods in Laravel.
That means that these are treated in the following way:

  1. getFullNameAttribute is stripped of get and attribute; leaving just FullName.
  2. This will then be converted into snake_case, resulting in full_name.
  3. This value is then added to an attributes array, which is accessible in the view, like a regular property.

Effectively, this will leave you with the following code.

$user = User::first();
echo $user->full_name;

This assumes you went with the first approach of:

public function getFullNameAttribute()
{
    return $this->first_name . ' ' . $this->last_name;
}

If you would like to access the property as $user->fullname, then you'd need to go for the following approach:

public function getFullnameAttribute()
{
    return $this->first_name . ' ' . $this->last_name;
}

Notice that we now have Fullname, with a lowercase n.
This is the case with all magic getters (and setters), they will be converted into snake_case, based on their PascalCase.

Interestingly, you could do the following and it would work:

getfull_nameAttribute()
{
    // ...
}

Relevant framework code.
All Laravel cares about is the get and Attribute bookends.

Comments

1

Define method in in your model

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

Then in query for example if you are fetching one record then

 $res= \App\Models\User::first();

   dd($res->fullName);

It means you are attribute is FullName then first letter become small letter while accessing

Comments

1

when you try

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

then inside model you need to add this as well

protected $appends = ['full_name'];

this you were missing then it will work ref link https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

1 Comment

You only need to do the $appends part if you're going to serialize the model.
1

That's not, how accessors work. You're treating it as a relation by using with(). Just remove the with() from your query and after querying the user, you can just access $user->full_name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.