How To Implement Pagination In PHP Laravel

Intermediate

2019-03-10

What is pagination ?

Pagination is the process of dividing your content into pages, this happens when you have a website and want to navigate  between content easily.

an example of pagination is at the end of this article where your can find a link to the previous and next article pages.

another common example is when you have a blog, typically you don't wan't to display all the blog posts at once unless you want to overwhelm the client browser and utilize a lot of your website resources.

for example

pagination

When to use pagination ?

Pagination follows a common concept in software engineering, don't load unnecessary data you don't need.

and that's the same principle from which lazy load came from.

If a user navigates to your blog page and he can see all the posts at once causing all the database posts table to be fetched, this would be a waste of resources. simply because this user is not guaranteed to read this amount of data at once, typically he would read up to 5 posts until he starts to navigate away from the page, so you would typically display 5 posts to the user.

What happens when he wants to read more ? Here comes the pagination. the user will find a way to navigate between pages and load more content.

How to develop pagination ?

Pagination can come in different shapes . e.g infinite scroll that you find in social media websites is a kind off pagination but you don't do any clicking action to load more pages. the User only has to continue scrolling down and the pages will be loaded infinitely.

Another example is blog pagination you find in this website and mentioned above.

This second kind is what we will implement in this tutorial.

What you need to start ?

In this tutorial we will be using php laravel framework v5 with MySQL database system.

How to fetch next and previous in pagination ?

The first solution that would come in mind is to do the classical three steps.

  • Find the specified object by key
  • order all the following objects ascending and take the first as the next post
  • order all the previous objects descending and take the first as the previous post

and an example implementation would be like this


What's wrong with this implementation ?

This implementation looks good and it serves the purpose but have several issues

  • It loads more data than needed by selecting all the attributes of the objects
  • It hits the DB engine unnecessarily ( we need three requests to get the objects )

Enhancement to the classic implementation

One of the solutions that I use is using left join.

left join allows you to query data from multiple tables it returns all records from the left table (table1), and the matched records from the right table (table2) if found otherwise the right table value will be NULL.

So in our case we will left join the posts table with itself twice. first to get the next post and another time to get the previous post


This solution solved the previous issues

  • We hit the DB engine only once
  • We lifted the work on the DB side
  • No unnecessary data is loaded
  • SQL Injection is protected against as DB::Select takes an array of where conditions that each will be escaped to prevent SQL Injection