alpha's avatar

How to paginate a collection?

Hello guys, I am having a hard time solving pagination problem.

Are there a way to paginate a collection? or how can i paginate a post while ordering it by created at?

Post::where('slug', $slug)->firstOrFail()->minipost()->orderBy('created_at', 'desc')->get();

This is the code that i use to retrieve the latest minipost for the post. But 1 post can have more then 15 minipost.. I want to paginate this so that in the first page i can see the latest 5 minipost and second page another 5 minipost

How can i solve this?

$post = Post::where('slug', $slug)->firstOrFail()->minipost()->orderBy('created_at', 'desc')->get();

$post->paginate(5); // not working Call to undefined method
0 likes
5 replies
coder's avatar
coder
Best Answer
Level 2

instead of doing with get() do like this

$post = Post::where('slug', $slug)->firstOrFail()->minipost()->orderBy('created_at', 'desc')->paginate(5);

and have a look to the docs as well http://laravel.com/docs/5.1/pagination

1 like
alpha's avatar

@coder oh :D Thanks alot.. I don't know that thing would do the trick

coder's avatar

@alpha
dude i don't have any idea about discuss, never worked on this .

Please or to participate in this conversation.