有没有办法按时间顺序排列时间戳使用laravel elquent?

时间:2016-06-23 10:19:02

标签: php mysql laravel laravel-4 eloquent

假设我想要一个按日期排序的查询,然后按其他列Testing Done排序,如下所示:

Item ID    Created At               Testing Done
-------------------------------------------------
5          2016-01-01 01:00:10      Not Done
1          2016-01-01 00:00:00      Done
2          2016-01-01 02:02:00      Done
4          2016-02-01 00:10:11      Not Done
3          2016-02-01 00:00:00      Done
6          2016-02-01 01:11:10      Done

即。该项目应首先按时间戳“创建时间”的日期部分排序,然后按测试完成顺序排序。

但是,当我尝试按此命令时:

$testingItem = TestingItem::orderBy('create_at', 'asc')->orderBy('testing_done', 'asc')->paginate(10);

结果如下:

Item ID    Created At               Testing Done
-------------------------------------------------
1          2016-01-01 00:00:00      Done
5          2016-01-01 01:00:10      Not Done
2          2016-01-01 02:02:00      Done
3          2016-02-01 00:00:00      Done
4          2016-02-01 00:10:11      Not Done
6          2016-02-01 01:11:10      Done

查询将created_at作为整个时间戳。有没有办法可以使用雄辩的laravel来构建查询以获得正确的结果?

2 个答案:

答案 0 :(得分:2)

这样做

$testingItem = TestingItem::orderByRaw('DATE(created_at)')->orderBy('testing_done', 'asc')->paginate(10);

答案 1 :(得分:1)

因为我没有任何经验。但根据mysql你可以尝试下面的

SELECT *, DATE(create_at) as date
FROM tablename

ORDER BY date ASC, testing_done ASC