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

mstnorris's avatar

Laravel 5: Eager Loading UsersController - view all users with their Roles and Permissions

Here is the index method on my UsersController

class UsersController extends Controller {

    /**
     * Returns all classmates
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $users = User::with('roles')->get();
        return view('users.index', compact('users'));
    }
}

I would also like to get the Permissions associated with the Roles and pass them to the view too. I know this may not be the best way to do it but for the time being we just want to show that it works.

0 likes
12 replies
Chris Magnussen's avatar
Level 3

Relations can be nested, so this would work:

$users = User::with('roles.permissions')->get();
Philipp's avatar

I'm having similar questions - related to this topic:

First of all - I'm using the roles package from @romanbican

I created a user and a role and via php artisan tinker i could establish a relationship.

$user->roles->lists('name'); //returns "Admin" - so it worked

Now i want to list all users with their roles on a page

Controller:

public function index() {

        // $users = User::with('roles')->get();

        $users = User::all();

        return view('users.index', compact('users'));
}

First question: No matter which $users i use above i get an output (array) in the view below? Whats the difference between those 2 types?

View:

@foreach ($users as $user)
                    
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ $user->roles }}</td>
    </tr>

@endforeach

Second question: How can i access the name of the role (for example "Admin") instead of getting the whole array? If i use

{{ $user->roles->lists('name') }}

i get an error:

ErrorException in helpers.php line 455:
htmlentities() expects parameter 1 to be string, array given (View:/home/vagrant/Code/uncrowned/resources/views/users/index.blade.php)

Thanks!

Philipp's avatar

Well the Error Message basically answered my own question (at least the second one :P)

Since a user can have many roles i also get an array back of roles... so i need to go over them as well.

@foreach ($users as $user)
                    
    <tr>
        <td>{{ $user->name }}</td>
        <td>

            @foreach ($user->role_list as $name)

                {{ $name }}
                                
            @endforeach

        </td>   
    </tr>
@endforeach

Dunno if it's ideal or if there's a better way to list my users with their roles.

Thanks

romanbican's avatar

@kungShu The difference between those 2 types it's really simple. First one will run query to get roles immediately. If you are using the second type and you will reference $users->roles somewhere in the code, the query to get all roles will be run at that specific moment. And if you want to get users roles two times in the same view, it will run query two times. So it isn't ideal.

User can have many roles, so you will always get an array of roles.

To get specific roles I believe this will work (but I don't know why would you do that):

$user->roles()->where('name', $name)->first();
Philipp's avatar

And all of a sudden the term "eager" loading makes complete sense! :D

Thanks again @romanbican

1 like
entreredes's avatar

I'm trying to use romanbican/roles

Can you help me how can I create my first role and attach it to a user.

I dont understand where I can do it.

Thx

mstnorris's avatar

I haven't used that package but how you'd do it using many-to-many relationships is:

$aRole->permissions()->attach($aPermission);

That is providing you have set up the relationships on the models correctly.

shadx's avatar

@romanbican: Is there an easy way to get all users for a specific role using your package?

bobbybouwmann's avatar

@shadx Congrats you just bumped a post that's 5 months old! If you have a question, create your own thread ;)

srikanthgopi's avatar

Are you using any roles/permissions package? i would like to do the same thing what you are trying to do in userscontroller

Please or to participate in this conversation.