laravel top 10 best pagination tips and tricks in 2022 | Pagination customization

laravel pagination tricks

This tutorial about laravel top 10 pagination tips in 2021. here also explain how to customize laravel pagination and get first or last page links on blade file. You read laravel pagination tutorials on advanced web tuts.

Laravel provide best pre define pagination methods to perform differet type of operations on pagination without write extra line of codes.


In this tutorial we discuss these laravel pagination tricks

  1. simple pagination
  2. paginate
  3. cursor pagination
  4. cursor vs offset pagination
  5. customize pagination url
  6. customize pagination links
  7. find current page
  8. next page url
  9. last page url
  10. find url of custom page

We explain these laravel 10 pagination tricks and tips step by step.

1. Simple pagination

Simple pagination is laravel pagination method which return only some information about pagination data. It only known how much data rows print on perpage, next and previous links. When we don't require to print total number of pages and only want next and previous buttons than why we load heavy paginate method so in that case we use simple pagination method. Below the given example how to calculate simple pagination.

$employees = Employee::where('age', '>', 45)->simplePaginate(10)

2. Paginate

Paginate is also a laravel pagination method but it take more execuation time compare to simple pagination and it contain more pagination customization methods so it is very important and more usefull. We simply pass per page show records in paginate() methhod and get on blade file it links by links() method. Read difference between paginate and simple pagination here

$employees = Employee::paginate(10)

3. Cursor pagination

Cursor pagination is a new pagination method which become in new version of laravel 8. Pagination and simple pagination method create queries by limit and offset method while cursor pagination method get data by where condition. Cursor pagination method use for large data scale and infinite num. of scrolls.

Cursor is use an encrypted pagination string instead of given page and perpage detail.

$employees = Employee::orderBy('id')->cursorPaginate(10);

4. Cursor vs offset pagination

In laravel simple pagination it use offset and limit and laravel cursor pagination use where condition and limit see in example. or offset pagination url show pages numbers where as cursor pagination show an encrypted string in url like ( http://localhost/employees?cursor=rtuiyJUiwI6MTUsIl9wb2GrtU9 8JNldGVtcyI6fhU8 ).

//offset pagination query
select * from employees order by id asc limit 10 offset 10;

//cursor pagination query
select * from employees where id > 10 order by id asc limit 10;

Cursor pagination best for large amount of data it search data according to id column which is an indexed column and its speed very fast

5. Customize pagination url

We also customize laravel pagination urls. These customization is use to send additional variables by click on pagination links. we take an example where pagination customize use

let supose we use search and pagination with normally when we search something and than click on pagination link than search data clear if we don't want to clear search data when use pagination than we append search variable on pagination url. see on example

$employees = Employee::paginate(10);

// now we append last search data on url
$searchValue = $request->searchValue;
$employees->appends(['searchValue' => $searchValue]);

//we display pagination links on blade file like
{{ $employees->links() }}

6. Customize pagination links

If you want to change laravel pagination default design than than follow these steps. For changing pagination design we make an saperate view file and make pagination design here. these we require some links like next and previous page link and all pages links. call laravel customize pagination view like

{{ $employees->links('admin.customPaginations') }}

In pagination view file call some variables to print next and previous links like

//print previous page here paginator is laravel pagination default variable
{{ $paginator->previousPageUrl() }}

//next page url
{{ $paginator->nextPageUrl() }}

7. Find current page

If you want to print current page url in laravel pagination than you are right palace see below example to print current page url in pagination.

$paginator->url($page)

8. Next page url

we find laravel next page url to go next page data in listing example for get next page url in below code

$paginator->nextPageUrl()

9. Last page url

last point we find next page url same way here we find last page url

$paginator->lastPage()

10. Find url of custom page

here we find find url of custom page where a custom page values is given and we find that page url by this method. $page is a variable which url we found

$paginator->url($page)
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.