Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SCC's avatar
Level 7

Retrieving a role or roles for an authenticated user

Hey,

So from another thread I ended up watching this video https://laracasts.com/lessons/users-and-roles

My current apps don't need roles as I am the only ever logged in user, however it got me thinking and I have questions that I find too many different answers and as I rarely use pivots I am confused.

Assuming I have setup a test of the above video I have my tables all set up and everything works as it should. If you have not seen it we have tables for users, roles and the role_user table.

In the User model the relationship is defined as:

    public function roles()
{
    return $this->belongsToMany('App\Role');
}

Ok so what I want to do then is Authenticate the current user and capture the role or roles for that user. I see people saying ->withPivot others saying it's wrong and so on.

Really I suppose there are two things here,

  1. Simple capture of the role or roles to display them in a view
  2. Authenticate the user with his role

Sorry if this seems simple but I guess I read too many other discussions that my head has too many ways of doing this.

Apologies for no code examples, on train heading home.

0 likes
4 replies
JarekTkaczyk's avatar

@HC-Lee withPivot may have its application here, but it has nothing to do with fetching related roles.

  1. I don't really get what issue you have, it's simple relation:
Auth::user()->roles; // collection of roles
  1. You don't authenticate role but user, so it doesn't matter which roles a user is in.
1 like
SCC's avatar
Level 7

Indeed, so if I for example use

Auth::user();

That will return nothing more than an array of whatever is in the user table for that user.

If I use

Auth::user()->roles;

That will return the array of whatever is in the roles table with a pivot which displays the role_user content.

If I can Authenticate the user and connect his/her roles I can use that to make sure they access parts that only allow their role and so on.

So I am not being stupid here, it really is that simple.

I need to stop searching things on Google and trusting other people's examples and just go with what I believe.

SCC's avatar
Level 7

Actually though, how for example would I get just the role name from the table and display it? Something like

{{  Auth::user()->roles->name;  }}

That would not work though would it? Sorry I need to stop trying to think this stuff out loud with a editor, I am not proficient enough on Laravel. I use it for very simple website things.

JarekTkaczyk's avatar

@HC-Lee I strongly encourage you to use artisan tinker REPL to test these things out. You just type and see the result, no need for a browser or whatsover.

And, yes, user->roles->name won't work, becuase roles is a collection. If you want names you can do roles->lists('name') // array of names. Otherwise you need to iterate through the collection and get name off of each role.

Please or to participate in this conversation.