我只有一个团队表和两个字段,外键指向那里。

DB::select('    SELECT
                    matches.*, teams.name as home , guests.name as guest
                FROM
                    matches
                LEFT JOIN
                    teams
                ON
                    teams.id = matches.home_id
                LEFT JOIN
                    teams as guests
                ON
                    guests.id = matches.guest_id
                ORDER BY
                    matches.date DESC, matches.time ASC
');

最佳答案

以下代码可以解决问题:

$results = DB::table('matches')
  ->leftJoin('teams', 'teams.id', '=', 'matches.home_id')
  ->leftJoin('teams as guests', 'guests.id', '=', 'matches.guest_id')
  ->orderBy('matches.date', 'desc')
  ->orderBy('matches.time', 'asc')
  ->addSelect('matches.*', 'teams.name as home', 'guests.name as guest')
  ->get();

关于mysql - 如何将该查询以 Eloquent /Laravel格式输入?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34250376/

10-13 02:05