Ad

Need To Convert Raw Queries Into Laravel Eloquent

- 1 answer

Below is my raw MySQL query:

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

Can any one help me to convert it to Eloquent? I am new to Laravel.

Ad

Answer

I solved it using DB::query() which is pretty straight forward

Ad
source: stackoverflow.com
Ad