How can I return one value from a belongsToMany relation.
This is my relation:
public function role() {
return $this->belongsToMany('App\Role');
}
Now when I want to access the Role Name I have to do the folowing:
Auth::user()->role[0]->name
But I just want to do
Auth::user()->role
But now my question is: "How can I do that?"
to do this you need add custom attribute to user model as the following:
User Model:
now you can use
Besides the question itself, i suggest you to use the plural name for belogsToMany relations: it is a common best practice and makes the code much more expressive.