I'm trying to understand how Laravel's Eloquent ORM works and was looking at the following MySQL query:
SELECT id, name, date FROM tablename GROUP BY name ORDER BY date
The use of GROUP BY
always returns the oldest values of name
. Is there a way to return the latest value instead?
Try following code,
Tablename::select('id', 'name', DB::raw('max(date) as latest_date'))
->groupBy('name')
->orderBy('latest_date')
->get()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With