【问题标题】:Laravel how to get all values with where conditionLaravel如何在where条件下获取所有值
【发布时间】:2020-12-14 18:57:49
【问题描述】:

此代码现在获取所有匹配项。我只需要在 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 条件。

【问题讨论】:

标签: php arrays laravel model-view-controller eloquent


【解决方案1】:

需要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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2022-10-06
    • 2014-04-21
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多