biniyam20's avatar

How to access length of total matched queries when using simplePaginate?

Hi!

I want to use simple Paginate to display my query results because I like the minimalistic UI of just to buttons labeled "next" and "previous". My users do not need more than that.

However, I would also like to be able to display the number of total rows matched to the search query. Normally using the pagination ->toal() method would give me that result however it is not available when using simple Paginate as stated in the docs: https://laravel.com/docs/5.7/pagination#paginator-instance-methods

I have tried using count($results) as well but it gives me the length of the paginated query not the length of the query without the pagination.

Any ideas?

Here's some modified code

//Controller
$listings = Listing::where('availability', 'Y')
                          ->orderBy('title', 'asc')
                          ->simplePaginate(100); // results in 1350 results

return view('browse.searchTable', [
                'listings' => $listings]);

//View
{{$listings->total()}} //method does not work for simple paginate
{{$listings->count()}} //returns 100
{{count($listings)}} //returns 100

//I want a solution that will return the number 1350 of the original query.

Now that I am writing this question in detail, does this mean I will have to write the same query in my controller to get the row count results and pass it to my view, something like

$count = SELECT COUNT(*) FROM 'listings' WHERE 'availability' = 'y';
//pass to view

Are there any performance drawbacks that come to mind from this approach?

Best, Biniyam

0 likes
8 replies
lostdreamer_nl's avatar

simplePaginate doesnt have a total ;)

If you really need it, you'll need to use the normal ->paginate() method, it will do 2 queries, 1 to get the total count, and 1 to get only the results of your current page. (so basically the same as doing the simplePaginate and your own count query)

biniyam20's avatar

@lostdreamer_nl Thanks for the quick response. I really want to keep the simple UI of the simple Paginate method (because that portion of my application requires really good and minimalistic UI as it's essentially the front page) so I think a better alternative for me might be to run the 2 queries myself and pass the total count.

I was just asking if anyone else had a different approach (potentially using the existing methods of simple pagination) while still using simple pagination, but as you stated since simple pagination only runs one query (with an offset) I doubt any combination of its method would give the total results.

shimax's avatar
$listings = Listing::where('availability', 'Y')
                          ->orderBy('title', 'asc')
                          ->simplePaginate(100);

$listings_total = Listing::count();
shimax's avatar
$listings_total = Listing::count();

this returns total for all the rows in the listings table.

biniyam20's avatar

Yeah, I got it thanks. I just re-queried for the total count.

  $rowCount = Listing::where('availability', 'Y')
                            ->count();
1 like
lostdreamer_nl's avatar
Level 53

If it's just about what the pagination looks like, you can run the command

php artisan vendor:publish

and select the Paginator to publish it's views to resources/views/vendor/pagination/

And change the way the pagination gets rendered.

Or ofcourse, running the count query yourself as you said... there's really no difference between running the query yourself or having the original paginator run that query.

If your indexes are correct, the count query should run within < 1 ms.

biniyam20's avatar

Yes @lostdreamer_nl As you are suggesting, I think it just comes down to a trade off between implementing how the paginator gets rendered through the command you shared or running a second query myself and considering the costs of running the query.

Thank you!

Please or to participate in this conversation.