Laravel 如何使用 where 条件获取所有值
Laravel how to get all values with where condition
此代码现在接受所有匹配项。我只需要在 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;
在这里帮助where条件。
需要 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();
此代码现在接受所有匹配项。我只需要在 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;
在这里帮助where条件。
需要 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();