Laravel sorting paginated content

paginate collection problem
Share on facebook
Share on twitter
Share on linkedin

Have you ever been in situation where you want to run sortBy method, to sort content by specific relation on object, but content is paginated also?

In that scenario you can only sort per page, and not overall result.

So we have:


and that would work perfectly, but the problem is simple, you can’t call ->paginate(x) on collection which get() returns.

At that scenario, it’s time to go to very top of your controller and to append following uses:

Next thing to do, is to implement following function which enables you to paginate collection, and it needs to look like:

And than, in that controller, you can use $this->paginate($exampleContent->get(), 10),  which will paginate your collection same way as ->paginate(10) would do on a query, and contain all same functions.

I’m totally sure that you can find better place for this function, but I’ve used controller just as quick example for educational purposes.

5 1 vote
Article Rating

Subscribe to our newsletter

Be first to read new helpful post

Share this post with your friends

Share on linkedin
Share on xing
Share on twitter
Share on facebook

And the winners are …

As already announced we are going to launch 42workflows in the near future – a free open source package that will help you automate your

symphony process

Symfony Process with Laravel

Are you using ‘exec’, ‘passthru’, ‘shell_exec’ and ‘system’ php functions in your Laravel project? Taking care of security risks by yourself, as well as expecting

Notify of
Inline Feedbacks
View all comments