Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get total number of results with pagination

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.

like image 723
PeterInvincible Avatar asked Feb 11 '15 23:02

PeterInvincible


People also ask

How can I get total records of pagination?

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

How can I get total count in laravel pagination?

You can create your custom view and use that. $results->count() => Get the number of items for the current page. This gives me how many items are there on the current page. I want the total number of pages.

Does pagination improve performance?

Thanks to pagination, we can split our large dataset into chunks ( or pages ) that we can gradually fetch and display to the user, thus reducing the load on the database. Pagination also solves a lot of performance issues both on the client and server-side!

What is pagination method?

The paginate method counts the total number of records matched by the query before retrieving the records from the database. This is done so that the paginator knows how many pages of records there are in total.


2 Answers

In Laravel 4 use:

{{ $users->getTotal() }} 

Docs


In Laravel 5 and above use:

{{ $users->total() }} 

Docs

like image 158
lukasgeiter Avatar answered Oct 06 '22 00:10

lukasgeiter


Controller

$products = ProductMaster::paginate(10); //1 page with 10 products  return view('index',compact('products')); 

view

 {{ $products->total() }}   //show total products in your database 
like image 43
Sunil kumawat Avatar answered Oct 06 '22 01:10

Sunil kumawat