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.
To Get All Item Total$paginator->total() Determine the total number of matching items in the data store. (Not available when using simplePaginate).
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.
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!
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.
In Laravel 4 use:
{{ $users->getTotal() }}
Docs
In Laravel 5 and above use:
{{ $users->total() }}
Docs
Controller
$products = ProductMaster::paginate(10); //1 page with 10 products return view('index',compact('products'));
view
{{ $products->total() }} //show total products in your database
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With