gpt4 book ai didi

php - 有没有办法在 laravel 中使用 eloquent 仅在时间戳列中按日期排序?

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:21 24 4
gpt4 key购买 nike

假设我想要一个按日期排序的查询,然后按另一列 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 的 Eloquent 来构建查询以获得正确的结果?

最佳答案

这样做

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

关于php - 有没有办法在 laravel 中使用 eloquent 仅在时间戳列中按日期排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37988809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com