- I have 3
id , msg and created_at in my model table. created_at is the timestamp, and id is the primary key. - I also have 5 data,
world => time4 , hello => time2 , haha => time1 , hihio => time5 and dunno => time3 , and this data is arranged in ascending order (as described here) based on their id .
In laravel 4 I want to get this data, sort it in ascending order and take the last n (in this case, 3) number of records. So, I want the dunno , world and hihio be displayed as follows in a div:
dunno,time3 world,time4 hihio,time5
What i tried
Model::orderBy('created_at','asc')->take(3);
unwanted result:
haha,time1 hello,time2 dunno,time3
Also tried
Model::orderBy('created_at','desc')->take(3);
unwanted result:
hihio,time5 world,time4 dunno,time3
I also tried the opposite with no luck
Model::take(3)->orderBy('created_at','asc');
This problem seems pretty simple, but I just can't figure out my logic. I'm still pretty new to Laravel 4, so I would give bonus points to better solutions than using orderBy() and take() , if any. Thank you very much!
sorting php mysql laravel laravel-4
Mark
source share