Bhumi Shah
Bhumi Shah

Reputation: 9476

getAttribute method is not calling laravel using query builder

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.

Upvotes: 0

Views: 2297

Answers (5)

BritishWerewolf
BritishWerewolf

Reputation: 3968

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.

Upvotes: 2

shaedrich
shaedrich

Reputation: 5735

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;

Upvotes: 1

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

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

Upvotes: 1

habeebdev
habeebdev

Reputation: 278

An accessor is not a relation. Try

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


dd($user->full_name);

Upvotes: 2

John Lobo
John Lobo

Reputation: 15319

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

Upvotes: 1

Related Questions