sajjadali's avatar

How to get total rows count in laravel with simplePagination()

How can I get total rows count with simplePagination() in Laravel?

paginate() runs two quires and both takes separate time to execute that I don't want.

Is there any other way to get total rows count in pagination ?

0 likes
13 replies
MichalOravec's avatar

$paginator->total() is not available when usingsimplePaginate. So only options is

$totalCount = ModelName::count();
sajjadali's avatar

it will return all rows not filtered. There is a filter before pagination which will obviously reduce rows

MichalOravec's avatar

Just use same filters

$totalCount = ModelName::where('foo', 'bar')->count();
sajjadali's avatar

it will return the total rows of first page/offset. for example if i do simpePaginate(10) it will return 10 or less to total from filtered results

sajjadali's avatar

but for each page. for example if i uses simplePaginate(10), it will show 10 on each page not total of filterd results

sajjadali's avatar

i want to show total rows of filtered results as the same thing we can achieve with paginate() like $users->total() this total() function is available for paginate not simplePaginate() and i need simplePaginate()

sajjadali's avatar

sorry this is not the answer. it is returning all from from table not filtered rows

jlrdw's avatar

Why don't you use regular paginate with a custom template. Fairly straight forward to do, but if you need an example, I will post.

But the above answer should work.

2 likes
sajjadali's avatar

already explained in my question. there is time issue. paginate() runs two queries and each take separate time (200ms each) and i want to save time as i can. if there is no solution, then I will obviously go with paginate()

Please or to participate in this conversation.