I'm migrating my scripts with Eloquent instead of SQL in my company.
I want to do this simple query with Eloquent, but I don't know how to do that.
SQL QUERY :
SELECT MAX(date), id
FROM myTable
WHERE people > 0
GROUP BY id
+--------------------------+
| date | ID |
+--------------------------+
| 2012-08-04 | 79 |
| 2013-04-13 | 56 |
| 2013-04-13 | 55 |
+--------------------------+
After several searches, the method with Eloquent that advised is:
MyModel::orderBy('date', 'desc')->groupBy('id')->where('people', '>', 0);
But the result is not (and it's normal...) the same :
+--------------------------+
| date | ID |
+--------------------------+
| 2012-06-25 | 79 |
| 2012-06-25 | 56 |
| 2012-06-25 | 55 |
+--------------------------+
I would simply like to know if it's possible to do a simple SELECT(MAX)
with Eloquent
, without using selectRaw()