Laravel:如何在按升序排序后获取最后 n(任意数量)行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24154208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel : How to take last n(any number) rows after ordered in ascending order?
提问by Mark
- I have 3 columns
id
,msg
andcreated_at
in my Model table.created_at
is a timestamp andid
is primary key. - I also have 5 datas,
world => time4
,hello => time2
,haha => time1
,hihio => time5
anddunno => time3
and these datas are arranged in ascending order (as arranged here) based on theirid
.
- 我有3列
id
,msg
并created_at
在我的模型表。created_at
是时间戳,id
是主键。 - 我也有5个DATAS, ,
world => time4
,,和这些的数据都是按升序排列根据他们(这里安排)。hello => time2
haha => time1
hihio => time5
dunno => time3
id
In laravel 4, I want to fetch these data, arrange them in ascending order and take the last n(in this case, 3) number of records. So, I want to get dunno
,world
and hihio
rows displayed like this in a div :
在 laravel 4 中,我想获取这些数据,按升序排列它们并获取最后 n(在本例中为 3)条记录。所以,我想在 div 中获取dunno
,world
和这样hihio
显示的行:
dunno,time3
world,time4
hihio,time5
What I have tried
我试过的
Model::orderBy('created_at','asc')->take(3);
undesired result :
不想要的结果:
haha,time1
hello,time2
dunno,time3
Also tried
也试过
Model::orderBy('created_at','desc')->take(3);
undesired result :
不想要的结果:
hihio,time5
world,time4
dunno,time3
I have also tried the reverse with no luck
我也试过相反的但没有运气
Model::take(3)->orderBy('created_at','asc');
This problem seems fairly simple but I just can't seem to get my logic right. I'm still fairly new in Laravel 4 so I would give bonus points to better solutions than using orderBy()
and take()
if there is. Thank you very much!
这个问题看起来很简单,但我似乎无法正确理解我的逻辑。我在 Laravel 4 中还是个新手,所以我会给比使用更好的解决方案加分orderBy()
,take()
如果有的话。非常感谢!
采纳答案by Tim Groeneveld
You are very close.
你很亲近。
It sounds like you want to first order the array by descending order
听起来您想先按降序对数组进行排序
Model::orderBy('created_at','desc')->take(3);
but then reverse the array. You can do this one of two ways, either the traditional PHP (using array_reverse).
但随后反转数组。您可以通过两种方式之一执行此操作,即传统的 PHP(使用 array_reverse)。
$_dates = Model::orderBy('created_at','desc')->take(3);
$dates = array_reverse($_dates);
Or the laravel way, using the reverse
function in Laravel's Collection
class.
或者 Laravel 方式,使用reverse
LaravelCollection
类中的函数。
$_dates = Model::orderBy('created_at','desc')->take(3)->reverse();
Check out Laravel's Collection
documentation at their API site at http://laravel.com/api/class-Illuminate.Support.Collection.html
Collection
在http://laravel.com/api/class-Illuminate.Support.Collection.html的 API 站点上查看 Laravel 的文档
Now $dates will contain the output you desire.
现在 $dates 将包含您想要的输出。
dunno,time3
world,time4
hihio,time5
回答by Kemal Fadillah
You're pretty close with your second attempt. After retrieving the rows from the database, you just need to reverse the array. Assuming you have an instance of Illuminate\Support\Collection
, you just need to the following:
您已经非常接近第二次尝试了。从数据库中检索行后,您只需要反转数组。假设您有一个 实例Illuminate\Support\Collection
,您只需要执行以下操作:
$expectedResult = $collection->reverse();
回答by Riowaldy Indrawan
$reverse = Model::orderBy('created_at','desc')->take(3);
$reverse = Model::orderBy('created_at','desc')->take(3);
$show = $reverse->reverse();
$show = $reverse->reverse();