40

I'd like to get the total number of users on a page where all the users are listed. This page should be paginated.

So far, this is what I've come up with:

Controller

$users = User::paginate(10);
return View::make('index', compact('users'));

View:

{{{ count($users) }}}

But this gives me only the count of the users for the current page. I'd like to count the full result set, if possible without querying another time the database.

PeterInvincible
  • 2,230
  • 5
  • 34
  • 62

6 Answers6

126

In Laravel 4 use:

{{ $users->getTotal() }}

Docs


In Laravel 5 and above use:

{{ $users->total() }}

Docs

mgraph
  • 15,238
  • 4
  • 41
  • 75
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
8

Controller

$products = ProductMaster::paginate(10); //1 page with 10 products

return view('index',compact('products'));

view

 {{ $products->total() }}   //show total products in your database
Sunil kumawat
  • 804
  • 8
  • 14
5

To Get All Item Total

$paginator->total() Determine the total number of matching items in the data store. (Not available when using simplePaginate).

To Get Current Page Total

$paginator->count() Get the number of items for the current page.

Example

Controller

$users = User::paginate(10); //1 page with 10 products
return view('users', compact("users"));

View:

{{ $users->total() }} //show total users in your database
THCBin
  • 21
  • 5
Chetan
  • 195
  • 2
  • 6
  • 2
    When adding an answer to a five year old question with an accepted answer it is important to point out what new aspect of the question your answer addresses. – Jason Aller Jul 16 '20 at 18:20
1
    // Assuming you have a paginated collection named $users
$total = $users->total();
$currentPage = $users->currentPage();
$perPage = $users->perPage();

$from = ($currentPage - 1) * $perPage + 1;
$to = min($currentPage * $perPage, $total);

echo "Showing {$from} to {$to} of {$total} entries";
Akshay ak
  • 63
  • 4
0

On Laravel 8x this data is also returned in the response.

https://laravel.com/docs/8.x/pagination#introduction

The JSON from the paginator will include meta information such as total, current_page, last_page, and more. The result records are available via the data key in the JSON array. Here is an example of the JSON created by returning a paginator instance from a route:

{
   "total": 50,
   "per_page": 15,
   "current_page": 1,
   "last_page": 4,
   "first_page_url": "http://laravel.app?page=1",
   "last_page_url": "http://laravel.app?page=4",
   "next_page_url": "http://laravel.app?page=2",
   "prev_page_url": null,
   "path": "http://laravel.app",
   "from": 1,
   "to": 15,
   "data":[
        {
            // Record...
        },
        {
            // Record...
        }
   ]
}
0

for total results {{ $results->total() }}

for the first item of current page {{ $results->firstItem() }}

for the last item of current page {{ $results->lastItem() }}

for current page {{ $results->currentPage() }}

Junaid Masood
  • 658
  • 11
  • 20