【发布时间】:2017-01-30 14:44:28
【问题描述】:
我将数据存储在我的数据库中。存储的数据是这样的
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
在我的控制器中,我试图检索不同的 upload_month,但获取每个最新插入的版本。目前我正在尝试
$uploadedFile = UploadedFile::groupBy('upload_month')->orderBy('created_at', 'desc')->get();
问题是这会返回以下内容
id | upload_month | created_at
-----------------------------------------
1 | January | 2017-01-30 13:22:39
-----------------------------------------
2 | Febuary | 2017-01-30 13:23:42
-----------------------------------------
因此,对于 1 月份的唱片,它提供的是旧版本。如果我将其更改为 ->orderBy('created_at', 'asc') 它会返回相同的记录,但 2 月是第一行。
本质上,我追求的是这个
id | upload_month | created_at
-----------------------------------------
1 | January | 2017-01-30 13:25:33
-----------------------------------------
2 | Febuary | 2017-01-30 13:23:42
-----------------------------------------
我怎样才能做到这一点?
谢谢
【问题讨论】:
-
我没有使用 distinct 的唯一原因是它正在执行原始数据库查询而不是使用 elequent
-
据我所知,Eloquent 没有进行任何查询
标签: laravel laravel-5 laravel-eloquent