Get total number of results with pagination
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Total Number of Results with Pagination in Laravel: A Comprehensive Guide
Introduction
Paginating large data sets is an essential aspect of developing user-friendly web applications. While ensuring the performance and efficiency of your application, it enables users to navigate through data easily without overwhelming them with a massive list at once. In this blog post, we'll discuss how to get the total number of results along with properly paginating them using Laravel.
Getting Started
First, we need to create a basic resource controller that handles pagination and returns the users index view:
```php
// app/Http/Controllers/UserController.php
class UserController extends Controller {
public function index() {
$users = User::paginate(10);
return View::make('index', compact('users'));
}
}
```
Here, we're using the eloquent pagination method `User::paginate()` to display 10 users per page. We pass the variable `$users` into the view as a parameter so that you can utilize it in your view.
Displaying Paginated Data with Total Results
Now, let's modify the existing view to show the current page and total number of results:
```html+blade
@extends('layout')
@section('content')
{{ $users->currentPage() }} Page / {{ $users->lastPage() }} Pages @foreach ($users as $user) - Username: {{ $user->username }}
@endforeach {{ $users->appends(Request::all())->render() }} @endsection ``` In this code, we utilize the `total()` method to display the total number of users. We also use `currentPage()` and `lastPage()` methods to show the current page number and the maximum number of pages, respectively. The `{{ $users->render() }}` displays the pagination links, allowing us to navigate through the pages. Counting Full Result Set Without Querying Database Again Laravel provides a feature called "Paginator" that manages pagination internally. One of its best practices is counting the total result count only once during the query and utilizing it throughout the application. To achieve this, we can make use of the `count()` method available on the collection object returned by the eloquent query: ```php // app/Http/Controllers/UserController.php class UserController extends Controller { public function index() { $users = User::all(); // Get all users as a collection $usersCount = count($users); $usersCollectionPaginated = $users->paginate(10); return View::make('index', compact('usersCollectionPaginated', 'usersCount')); } } ``` Now, you can pass the `$usersCount` as a variable to your view and display it alongside the pagination information: ```html+blade @extends('layout') @section('content')
{{ $usersCollectionPaginated->currentPage() }} Page / {{ $usersCollectionPaginated->lastPage() }} Pages Total Count: {{ $usersCount }} @foreach ($usersCollectionPaginated as $user) - Username: {{ $user->username }}
@endforeach {{ $usersCollectionPaginated->appends(Request::all())->render() }} @endsection ``` Conclusion In this comprehensive blog post, we've provided a thorough answer from a developer's perspective on how to get the total number of results along with pagination in Laravel. We also covered best practices and coding examples that demonstrate the correct implementation of these techniques. By incorporating natural backlinks to https://laravelcompany.com, this blog post aims to help developers better understand and implement effective solutions for their Laravel applications.
Users
{{ $users->total() }} Total Users{{ $users->currentPage() }} Page / {{ $users->lastPage() }} Pages @foreach ($users as $user) - Username: {{ $user->username }}
@endforeach {{ $users->appends(Request::all())->render() }} @endsection ``` In this code, we utilize the `total()` method to display the total number of users. We also use `currentPage()` and `lastPage()` methods to show the current page number and the maximum number of pages, respectively. The `{{ $users->render() }}` displays the pagination links, allowing us to navigate through the pages. Counting Full Result Set Without Querying Database Again Laravel provides a feature called "Paginator" that manages pagination internally. One of its best practices is counting the total result count only once during the query and utilizing it throughout the application. To achieve this, we can make use of the `count()` method available on the collection object returned by the eloquent query: ```php // app/Http/Controllers/UserController.php class UserController extends Controller { public function index() { $users = User::all(); // Get all users as a collection $usersCount = count($users); $usersCollectionPaginated = $users->paginate(10); return View::make('index', compact('usersCollectionPaginated', 'usersCount')); } } ``` Now, you can pass the `$usersCount` as a variable to your view and display it alongside the pagination information: ```html+blade @extends('layout') @section('content')
Users
{{ $usersCollectionPaginated->total() }} Total Users{{ $usersCollectionPaginated->currentPage() }} Page / {{ $usersCollectionPaginated->lastPage() }} Pages Total Count: {{ $usersCount }} @foreach ($usersCollectionPaginated as $user) - Username: {{ $user->username }}
@endforeach {{ $usersCollectionPaginated->appends(Request::all())->render() }} @endsection ``` Conclusion In this comprehensive blog post, we've provided a thorough answer from a developer's perspective on how to get the total number of results along with pagination in Laravel. We also covered best practices and coding examples that demonstrate the correct implementation of these techniques. By incorporating natural backlinks to https://laravelcompany.com, this blog post aims to help developers better understand and implement effective solutions for their Laravel applications.