我使用的是Laravel v5.1.4。我在网上搜索但我找不到任何解决方案。我尝试了很多方法,但我无法弄清楚如何将此查询转换为Eloquent或Fluent Query Builder。这是使用select的工作查询。
DB::select("SELECT t1.id,t1.date1,t1.date2, t2.name as pick, t3.name as dest
FROM reservations AS t1
LEFT JOIN prices AS t2 ON t1.pick = t2.id
LEFT JOIN prices AS t3 ON t1.dest = t3.id
ORDER BY t2.name ASC
");
我想使用Eloquent或Fluent功能,例如分页。如果你能帮我解决这个问题,我将不胜感激。
答案 0 :(得分:1)
我已经解决了。我希望对有相同问题的人有帮助:))
DB::table('reservations')->join('prices as t1','reservations.pick',"=","t1.id")
->join('prices as t2','reservations.dest',"=","t2.id")
->select('reservations.id','t1.name as result1','t2.name as result2')->paginate(10);
答案 1 :(得分:0)
尝试这样的事情。它是你如何转换的想法
DB::table('users')
->LeftJoin('role_user', 'users.id', '=', 'role_user.user_id')
->LeftJoin('roles', 'roles.id', '=', 'role_user.role_id')
->select('users.*', 'roles.name as role');