Get total number of results with pagination

PeterInvincible picture PeterInvincible · Feb 12, 2015 · Viewed 33.6k times · Source

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.

Answer

lukasgeiter picture lukasgeiter · Feb 12, 2015

In Laravel 4 use:

{{ $users->getTotal() }}

Docs


In Laravel 5 use:

{{ $users->total() }}

Docs