需要将原始查询转换为laravel Eloquent

时间:2016-01-21 12:15:05

标签: mysql sql laravel eloquent laravel-3

以下是我的原始MySQL查询:

SELECT T1.id, T1.department_id, COALESCE(T2.cnt, 0) AS cnt 
 FROM (SELECT DISTINCT id, department_id FROM users WHERE company_id = '33') AS T1
    LEFT JOIN ( 
          SELECT user_id, COUNT(*) AS cnt 
               FROM userlogs 
               WHERE created_at BETWEEN '2015-12-17' AND '2016-01-12' 
               GROUP BY user_id 
              ) AS T2 ON T2.user_id = t1.id

任何人都可以帮我转换为Eloquent吗?我是Laravel的新手。

1 个答案:

答案 0 :(得分:1)

我使用DB::query()解决了这个问题非常简单