Laravel如何在where条件下获得所有值



此代码现在接受所有匹配项。我只需要在home_team_score和away_team_score不为空的地方:

$matches = Match::with('score', 'homeTeam', 'awayTeam')->
where('league_id', '=', $league->id)->get();
foreach ($matches as $match) {
$homeTeamScore = $match->score->home_team_score;
$awayTeamScore = $match->score->away_team_score;

请在此处帮助了解条件。

需要whereHas来约束查询,使其仅包括home_team_score和away_team_score不为空的匹配项。

$matches = Match::with('score', 'homeTeam', 'awayTeam')
->whereHas('score', function($query){
$query->whereNotNull('home_team_score')
->whereNotNull('away_team_score');
})
->where('league_id', '=', $league->id)
->get();

最新更新