【发布时间】:2014-07-31 23:54:29
【问题描述】:
- 我的模型表中有 3 列
id、msg和created_at。created_at是时间戳,id是主键。 - 我还有 5 个数据,
world => time4、hello => time2、haha => time1、hihio => time5和dunno => time3,这些数据根据它们的id升序排列(如此处排列)。李>
在 laravel 4 中,我想获取这些数据,将它们按升序排列并取最后 n(在本例中为 3)条记录。所以,我想在 div 中像这样显示 dunno,world 和 hihio 行:
dunno,time3
world,time4
hihio,time5
我的尝试
Model::orderBy('created_at','asc')->take(3);
不想要的结果:
haha,time1
hello,time2
dunno,time3
也试过了
Model::orderBy('created_at','desc')->take(3);
不想要的结果:
hihio,time5
world,time4
dunno,time3
我也尝试了相反的方法,但没有运气
Model::take(3)->orderBy('created_at','asc');
这个问题看起来相当简单,但我似乎无法正确理解我的逻辑。我在 Laravel 4 中还是相当新的,所以如果有的话,我会给比使用 orderBy() 和 take() 更好的解决方案加分。非常感谢!
【问题讨论】:
-
使用
orderBy('created_at', 'desc')->take(3),然后反转数组。
标签: php mysql sorting laravel laravel-4