简体   繁体   中英

How to join 3 table eloquent laravel 8

i have 3 tables

user

user_id
name_user
contacts
request_id

request_register

request_id
user_id
team_id

team

team_id
name_team

i want get list of user details who register to team with spesific id, example:

team with id 1

id_user name_user contacts team_id name_team
1 michael 1234 1 phoenix
2 john 1232 1 phoenix
3 cindy 1222 1 phoenix

You can use whereHas() method provided by Laravel, but you have to specify the relationships needed to query using the method, probably like this:

$teamId = //retrieve the team id...;
$users = User::whereHas('request_register', function() (Builder $query) use ($teamId) {
   $query->where('team_id', $teamId);
})->get();

This will automatically make a join between users and request_register to get what you want to get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM