Back to Basics: Laravel Pagination in 2 Minutes

Laravel has a lot of useful functions in the core, one of them is a simple pagination for your table list of entries. Let’s see how it works.

Notice: This is a chapter from upcoming mini-course I’m creating now called “How to create admin panel in Laravel”, which will be published later this week on, but decided to publish some parts as separate short articles. Follow the launch of the full course on Twitter.

Let’s say we have a list table of authors, with this code.


And /resources/views/authors/index.blade.php:

Here’s the visual result of it all.

laravel pagination 01

What if we have more entries and need to have pagination?
First, we use paginate() method in Controller instead of all():

Next – to display the pagination in our list in the file resources/views/authors/index.php we just use method links() like this:

In case of 1-5 entries, it won’t display anything, but beyond that we will see something like this:

laravel pagination 02

Guess what – if we click “Page 2” link or the arrow to the right, it will actually work!

laravel pagination 03

As you can see, paginator will add a GET parameter to the URL ?page=2, and we don’t need to add any custom code to make it work. Isn’t that awesome?

Of course, you can perform a lot of customizations with this pagination – move the page length to config, change URL parameters and add your own ones, apply different styling than default etc. But I will leave it for you to play around, if you wish. More information about pagination in Laravel can be found in the official documentation.

Want to generate Laravel adminpanel online?
You don't need any packages to do that!

2 thoughts on “Back to Basics: Laravel Pagination in 2 Minutes

Leave a Reply

Your email address will not be published. Required fields are marked *