I have 2 tables users
and roles
, I'm trying with laravel to get the name of the role which my user owns, this is my Model
of roles
class Role extends Model
{
protected $table = 'roles';
public function user(){
return $this->hasMany('App\User');
}
}
Relation Model User
public function role(){
return $this->hasMany('App\Role', 'id');
}
I try to get the data using:
$user = \Auth::user();
echo $user->id_role->name;
If I use only:
echo $user->id_role;
brings me the data without problems, the drawback is to get the data from the role table. I throw the error
Trying to get property of non-object