Get total number of results with pagination

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

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:

php
@extends('layout')

@section('content')
<h1>Users</h1>

{{ $users->total() }} Total Users <br />
{{ $users->currentPage() }} Page / {{ $users->lastPage() }} Pages

@foreach ($users as $user)
  - Username: {{ $user->username }} <br />
@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:

php
@extends('layout')

@section('content')
<h1>Users</h1>

{{ $usersCollectionPaginated->total() }} Total Users <br />
{{ $usersCollectionPaginated->currentPage() }} Page / {{ $usersCollectionPaginated->lastPage() }} Pages

Total Count: {{ $usersCount }}

@foreach ($usersCollectionPaginated as $user)
  - Username: {{ $user->username }} <br />
@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.