Q: 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.

A: An accessor is not a relation. Try
$user = User::where('user_id', $id)->first();


dd($user->full_name);


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

*

*getFullNameAttribute is stripped of get and attribute; leaving just FullName.

*This will then be converted into snake_case, resulting in full_name.

*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.

A: 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;

A: 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

A: 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
