如何将此查询转换为Eloquent语法
SELECT x.id, x.title, points, (points - 1)/POW(((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(x.created_at))/3600)+2, 1.5) as hasil
FROM media x
JOIN (SELECT m.id,
(SUM(mp.up)-SUM(mp.down)) AS points
FROM media m
JOIN media_points mp ON mp.media_id = m.id
GROUP BY m.id) y ON y.id = x.id
ORDER BY hasil DESC
LIMIT 100
答案 0 :(得分:2)
最后我通过使用raw()
找到了答案Media::select(DB::raw('media.*, (points - 1)/POW(((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(media.created_at))/3600)+2, 1.5) as hasil'))
->join(DB::raw('(SELECT m.id, (SUM(mp.up)-SUM(mp.down)) AS points FROM media m JOIN media_points mp ON mp.media_id = m.id GROUP BY m.id) as T'), 'T.id', '=', 'media.id')
->orderBy('hasil','desc')
->take(100)