Support the ongoing development of →
Eloquent Database
Last updated 1 year ago.

If you have followed laravel documentation and have defined belongsToMany relations you could do simply:

$users = User::with("roles")->all();

So i don't think it's really necessary to wrap it in a method in the user model.

For completeness:

foreach($users as $user){
    foreach($user->roles as $role){
        //here you have access to all user fields and related roles fields
Last updated 1 year ago.

Thanks this one worked for me .... :-)

can you help me for this complex query on the below given link

Last updated 1 year ago.

Sign in to participate in this thread!


Your banner here too?

mukeshp004 mukeshp004 Joined 23 Jun 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.