- 我在我的模型表中有3列
id
,msg
和created_at
。created_at
是時間戳,id
是主鍵。 - 我也有基於其
id
5個DATAS,world => time4
,hello => time2
,haha => time1
,hihio => time5
和dunno => time3
和這些的數據都是按升序排列(如下安排)。
在laravel 4中,我想要獲取這些數據,按升序排列它們並取最後n個(本例中爲3)個記錄。所以,我想在一個div像這樣顯示dunno
,world
和hihio
行:Laravel:如何按升序排列後的最後n個(任意數量)行?
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)',然後顛倒數組。 –