Skip to main content
Version: 1.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.

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();

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(); // 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(); // Array of 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('bootstrap') !!}

// tailwind theme
{!! $filteredUsers->links('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($theme = null)Returns HTML pagination links.

Pagination Themes

The following pagination themes are available:

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