Skip to main content
Version: 2.x

Pagination

Pagination in Firestore Eloquent Library

Pagination is a crucial feature when dealing with large datasets, and the Firestore Eloquent Library in Laravel provides easy-to-use methods for paginating query results. Let's explore how to implement pagination using the library.

Note

With version >= 2.x of this Library, if you want to return an array of paginated data instead of html , you can pass the array as an argument to the links() method. Example $users->links('array'). This will return an array of pagination links instead of HTML.
See more examples below.

Paginating Results with paginate()

The paginate() method allows you to retrieve a specific number of documents per page, making it easier to navigate through large result sets.

The paginate() method accepts two arguments:

  • The number of documents to retrieve per page
  • The name of the page query parameter

Basic Pagination

Let's paginate the users collection by retrieving 10 documents per page:

// Paginate all users with 10 users per page
$users = User::paginate(10);

This retrieves the first 10 users and provides pagination information. The pagination information is stored in the $users variable, and you can access it using the links() method or pagination() method:

// Paginate all users with 10 users per page
$users = User::paginate(10);

// Get pagination links
$users->links();

// Get pagination information
$users->pagination();

Array Pagination

Credit to Oburusule Dunstan for the array pagination implementation.

You can return an array of paginated data instead of HTML by passing the array as an argument to the links() method:
It is important to note that the array pagination is only available in version >= 2.x of this library and it is recommended when working with APIs or InertiaJS applications where you need to return the pagination links as an array.

// Paginate all users with 10 users per page
$users = User::paginate(10);

// Get pagination links as an array
$users->links('array');

//Results
/*
[▼
"links" => [
[▼
"url" => "http://127.0.0.1:8000/?page=1"
"label" => "1"
"active" => true
]
[▼
"url" => "http://127.0.0.1:8000/?page=2"
"label" => "2"
"active" => false
]
]
"prev" => null
"next" => "http://127.0.0.1:8000/?page=2"
"total_pages" => 2
"per_page" => 4
"current_page" => 1
"last_page" => 2
"total_data" => 8
]
*/

This will return an array of pagination links instead of HTML.

Customizing Page Parameter Name

By default, the page query parameter name is page. You can customize this by passing the name of the page query parameter as the second argument to the paginate() method:

// Paginate all users with 10 users per page
$users = User::paginate(10, 'p');

// Get pagination links
$users->links();

// Get pagination information
$users->pagination();

Accessing Paginated Data

You can access the paginated data and other useful information:

// Accessing paginated data
$data = $users->data(); // Object of user data for the current page

$data = $users->toArray(); // Array of user data for the current page

You can display the pagination links using the links() method:

{{-- Displaying pagination links --}}
{!! $users->links() !!}

Displaying Pagination Information

You can display the pagination information using the pagination() method:

{{-- Displaying pagination information --}}
$users->pagination()

Filtered Pagination

Basic Filtered Pagination

// Paginate users with age 25, 10 users per page
$filteredUsers = User::where(['age', '=', 25])->paginate(10);

In this example, the where() method is used to filter users with an age of 25, and the paginate() method is then applied to paginate the results, showing 10 users per page.

Customizing Page Parameter Name

Similar to regular pagination, you can customize the name of the page query parameter:

// Paginate users with age 25, 10 users per page
$filteredUsers = User::where(['age', '=', 25])->paginate(10, 'p');

Accessing Paginated Data

You can access the paginated data and other useful information:

// Accessing paginated data
$data = $filteredUsers->data(); // User data for the current page

You can display the pagination links using the links() method:

{{-- Displaying pagination links --}}

// default theme by firestore eloquent library
{!! $filteredUsers->links() !!}

// bootstrap theme
{!! $filteredUsers->links(theme: 'bootstrap') !!}

// tailwind theme
{!! $filteredUsers->links(theme: 'tailwind') !!}

Displaying Pagination Information

You can display the pagination information using the pagination() method:

{{-- Displaying pagination information --}}
$filteredUsers->pagination()

Customizing Pagination Views

Customize the pagination views by using the data provided by the pagination() method:

$filteredUsers->pagination()

//Results
/*
[
"total_pages" => 2,
"per_page" => 10,
"current_page" => 1,
"last_page" => 2,
"next_page" => 2,
"prev_page" => null,
"from" => 0,
"to" => 10,
"total" => 20 ,
];
*/

Pagination Methods

The following methods are available on the paginate() method:

MethodDescription
data()Returns an array of paginated data.
pagination()Returns an object with pagination information.
count()Returns the total number of pages.
currentPage()Returns the current page number.
firstItem()Returns the starting index of the paginated data.
hasMorePages()Returns true if there are more pages, false otherwise.
totalPages()Returns the total number of pages.
lastItem()Returns the ending index of the paginated data.
lastPage()Returns the last page number.
nextPageUrl()Returns the URL for the next page.
previousPageUrl()Returns the URL for the previous page.
onFirstPage()Returns true if on the first page, false otherwise.
url($page)Returns the URL for a specific page.
total()Returns the total number of items.
getPageName()Returns the name of the page query parameter.
links($type="html", $theme = null)Returns HTML pagination links by default. Pass array as an argument to return an array of pagination links. Themes available are null, bootstrap, and tailwind.

Pagination Themes

The following pagination themes are available:

ThemeDescription
nullDefault theme by firestore eloquent library.
bootstrapBootstrap theme.
tailwindTailwind theme.