[英]How to join 3 table eloquent laravel 8
i have 3 tables我有 3 张桌子
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:我想获取使用特定 ID 注册到团队的用户详细信息列表,例如:
team with id 1 ID 为 1 的团队
id_user用户名 | name_user用户名 | contacts联系人 | team_id team_id | name_team名称_团队 |
---|---|---|---|---|
1 1 | michael迈克尔 | 1234 1234 | 1 1 | phoenix凤凰 |
2 2 | john约翰 | 1232 1232 | 1 1 | phoenix凤凰 |
3 3 | cindy辛迪 | 1222 1222 | 1 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:你可以使用Laravel提供的whereHas()方法,但是你必须指定使用该方法查询所需的关系,大概是这样的:
$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.这将自动在users
和request_register
之间进行连接以获得您想要的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.