I have Category Model like below
class Category extends Model
{
public function users(){
return $this->hasMany(User::class)->where("active_status",1)->where("user_type", 'user');
}
}
I have User Model like below
class User extends Authenticatable
{
public function getFullName()
{
return $this->first_name.' '.$this->middle_name.' '.$this->last_name;
}
}
My controller code is like below
$category = Category::join('users', 'users.category_id', '=', 'categories.id')->get();
I am trying to use below code in view
@foreach( $result as $spc )
$spc->getFullName();
@endforeach
I am getting error like below
[2019-09-24 14:45:43] laravel.ERROR: Call to undefined method App\Category::getFullName()
$spcis aCategory, not aUser, and you've defined the function on yourUserclass. Also,getFullName()should begetFullNameAttribute(), and calling$user->full_namewill magically return the full name. See accessors: laravel.com/docs/5.8/eloquent-mutators#defining-an-accessorCategory::with('users')->get()) and show it in your view ($spc->user->getFullName())HasManyrelation