Support the ongoing development of Laravel.io →
Eloquent Database
Last updated 2 years ago.
0

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 2 years ago.
0

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

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

http://laravel.io/forum/10-06-2014-how-to-use-join-on-more-tha...

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mukeshp004 mukeshp004 Joined 23 Jun 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.