【问题标题】:Laravel query how to join data from multimple tablesLaravel查询如何连接来自多个表的数据
【发布时间】:2021-04-11 08:30:03
【问题描述】:

在 laravel 查询中加入多个表需要帮助

Matches table   

id
round
league_id
home_team_id
away_team_id
match_date


Match facts table   
id
match_id
player_id
minutes
goals

此查询将主队和客队球员聚集在一起:

$MatchFacts = Match_fact::where('match_id', $match->id)
    ->get();

目标是通过团队成员获取所有比赛事实。

【问题讨论】:

标签: database laravel model-view-controller laravel-query-builder laravel-controller


【解决方案1】:

这个查询解决了问题:

$away_team_facts = DB::table('match_facts')
            ->where('match_id', $match->id)
            ->join('players', 'match_facts.player_id', '=', 'players.id')
            ->where('players.team_id', '=', $awayTeamId)
            ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    相关资源
    最近更新 更多