我尝试在Eloquent查询中转换以下SQL请求:
SELECT locals.*
FROM prises JOIN locals
ON prises.liaison_id = locals.id
GROUP BY locals.id
ORDER BY COUNT(liaison_id);
我写了这个:
return $query->select('locals.*')
->from('prises')
->join('locals', function($join) {
$join->on('prises.liaison_id', '=', 'locals.id');
})
->orderBy(DB::raw('count(prises.liaison_id)', 'DESC'))
->groupBy('locals.id');
在我的php页面上,它可以运行,但此查询正在运行:
select "locals".* from "prises"
inner join "locals" on "prises"."liaison_id" = "locals"."id"
group by "locals"."id"
order by count(prises.liaison_id) asc limit 100 offset 0
我的Asc / Desc是固定的,我不知道为什么。使用DB :: raw阻止我吗?
提前致谢。