Eloquent获得与模型X相关的所有模型
问题描述:
The problem I'm facing is that I want all matches belonging the to the player for a specific season. I'm using Eloquent in combination with Slim 2.
This is my ERD:
Is this even possible? I was thinking of start with the season and that way I can go down but than I face the problem of the fact a match can have multiple players and I would love to use the player model to call all his matches.
我面临的问题是我希望所有比赛属于特定赛季的球员。 我正在将Eloquent与Slim 2结合使用。 p>
这是我的ERD: p>
p>
这是否可能? 我想从赛季开始,这样我就可以下来,但是我面对的问题是比赛可以有多个球员,我很乐意用球员模型来召唤他所有的比赛。 p> \ n div>
答
Easier - when you have season id
:
$player->matches()->whereHas('game', function ($q) use ($seasonId) {
$q->where('season_id', $seasonId);
})->get();
A bit longer with just its name:
$player->matches()->whereHas('game.season', function ($q) use ($seasonName) {
$q->where('name', $seasonName);
})->get();