Support the ongoing development of Laravel.io →
Eloquent Database

user table

id name username email password

role

id name

role_detail

role_id -> primary of role user_id -> primary of user module_id menu_id permission

required query to convert in eloquent

Select role_detail., role.name, user. from user join role_detail on role_detail.user_id = user.id join role on role.id = role_detail.role_id

how can i define above query in eloquent in user model

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-thane-3-table-using-eloquent-convert-the-above-query-in-eloquent-way

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.

© 2025 Laravel.io - All rights reserved.