lundi 30 janvier 2017

Distinct most recent data from database

I am storing data in my database. The data being stored looks like this

id  | upload_month | created_at
-----------------------------------------
1   | January      | 2017-01-30 13:22:39
-----------------------------------------
2   | Febuary      | 2017-01-30 13:23:42
-----------------------------------------
3   | January      | 2017-01-30 13:25:33

Within my Controller I am trying to retrieve the distinct upload_month, but get the latest inserted version for each. At the moment I am trying

$uploadedFile = UploadedFile::groupBy('upload_month')->orderBy('created_at', 'desc')->get();

The problem is that this is returning the following

id  | upload_month | created_at
-----------------------------------------
1   | January      | 2017-01-30 13:22:39
-----------------------------------------
2   | Febuary      | 2017-01-30 13:23:42
-----------------------------------------

So for the January record it is giving the older version. If I change it to ->orderBy('created_at', 'asc') it returns the same records but Febuary being the first row.

In essense, what I am after is this

id  | upload_month | created_at
-----------------------------------------
1   | January      | 2017-01-30 13:25:33
-----------------------------------------
2   | Febuary      | 2017-01-30 13:23:42
-----------------------------------------

How am I able to achieve this?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire