将子选择转换为Eloquent查询

时间:2013-06-21 19:51:38

标签: laravel laravel-4 eloquent

我有这个Subselect查询,我希望能够转换为Eloquent查询。有人可以帮忙吗?

$query = 'SELECT * FROM ( SELECT * FROM `visits` WHERE `exit` = 0 ORDER BY `updated_at` DESC ) visits GROUP BY `visits`.`session_id`';

由于

1 个答案:

答案 0 :(得分:1)

查询使用了连接:

Visit::select('visits.*')->
join(DB::raw('(SELECT * FROM `visits` WHERE `exit` = 0 ORDER BY `updated_at` DESC, `id` DESC) v2'), 'visits.id', '=', 'v2.id')->
groupBy('visits.session_id')->get();