[英]querying many-to-many-relationship laravel
我与模型Competition和Season有一个多对多关系,其中有一个名为Competition_season的数据透视表。
季节->属于Tomany->竞赛-> hasMany->团队-> hasMany->玩家
我想查询特定季节中团队中所有球员的数据。
就像是
\App\Player::with('team.competition.season')->where('team.competition.season.id', 2);
但是由于此时season是一个集合(竞争有很多季节),所以我永远不能这样查询它。
有人可以为我指出正确的方向,以便在特定季节正确选择团队中的所有球员吗?
首先,您需要在模型中添加关系hasManyTrough(有关更多信息,请参见: has-many-through )然后,您需要的只是“ whereHas”
$players = \App\Player::with('season')->whereHas('season', function($q){
$q->where('season.id', '=', 2);
})->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.