1

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.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
Malarivtan
  • 404
  • 1
  • 6
  • 20
  • 2
    Are you really using `laravel-3`? Also what have you tried? – Mark Davidson Jan 21 '16 at 14:40
  • Please tell us what you have tried? – Panagiotis Koursaris Jan 21 '16 at 16:00
  • Laravel 3 is not supported any more – Ozan Kurt Jan 21 '16 at 17:57
  • @Ozan: I know Laravel 3 is supported but my application was built in Laravel 3 and we are planing to move to laravel 5.2 this year. however, business needs to run as usual. – Malarivtan Jan 22 '16 at 08:19
  • @Panagiotis and Mark: I tried with Fluent but Pure Eloquent syntax is little different and I am new to it so that is why asking for help. I made it work by using DB::Query(), really wanted to learn how to do it using MODEL:: – Malarivtan Jan 22 '16 at 08:21
  • @Ozan IE7 is not supported anymore either but it doesn't mean there are no IE7 users. Big companies, like Yasar Holding, Akzo Nobel etc still uses Windows NT in their workstations. It is a huge expense for a big company to change their infrastructure at once. So same for laravel because Laravel doesn't support 3 anymore doesn't mean there are nobody out there L3 apps, most of them still in old servers that they can't upgrade PHP or their server or simply rewrite the entire application because with Laravel 4 they changed the entire system that a simple upgrade is not an option. – serdar.sanri Apr 07 '16 at 21:23
  • Company I work has over 300 State websites and upgrading PHP is a big deal that most sites has different structures, some of them WP, Drupal, Laravel or custom CMS. Anyway, @tanvir I know you solved the issue with raw query but with eloquent it would have been really harder. Especially with subqueries laravel eloquent may cause you lots of headaches. Easiest way to it rather than raw query would have been separating each table into different models and use eager loading, Another way might be creating a View in mysql server and using as a Model source in eloquent. – serdar.sanri Apr 07 '16 at 21:24

1 Answers1

1

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

Malarivtan
  • 404
  • 1
  • 6
  • 20