LaraBABA's avatar

Quick question on pagination with laravel 5.5

Hello,

When I used to code my pagination away from frameworks, I used to create 2 queries: 1 query that counts the number of record in the given table, the output of this query was populating the "previous no1, no2,no3....next" pagination area

And the second query was outputting the record with a limit, let's say 15 at the time.

By doing so, my paginations were pretty quick.

I understand that there is eager loading as explained in other threads but I would also like to know if the "DB:table" queries work like the above example or do they count all the rows in the table which output both the pagination incrementation + records, if yes then it will be quite inefficient.

Thank you.

0 likes
5 replies
Muntadher's avatar

when you convert the record to json you will see

{
   "total": 50, // all num
   "per_page": 15, 
   "current_page": 1,
   "last_page": 4,
   "first_page_url": "http://laravel.app?page=1",
   "last_page_url": "http://laravel.app?page=4",
   "next_page_url": "http://laravel.app?page=2",
   "prev_page_url": null,
   "path": "http://laravel.app",
   "from": 1,
   "to": 15,
   "data":[]
}

to get total use $result->total();

more information https://laravel.com/docs/5.5/pagination#paginator-instance-methods

1 like
Snapey's avatar

The 'length-aware' paginator will count all matching records so that it can show the number of pages and display the appropriate links.

Because this may not be appropriate in all cases, there is also a 'simple' paginator which just does a simpler query and only allows a next and previous link.

https://laravel.com/docs/5.5/pagination#basic-usage

1 like
jlrdw's avatar
jlrdw
Best Answer
Level 75

@Boubou see this https://laracasts.com/discuss/channels/guides/length-aware-paginator

Skip and take and a pre count make the Lengthaware paginator very efficient.

Eager load just means load when needed, i.e., You have a company and want to list their accounts receivables.

That orm breaks down very fast if in a nested foreach the inner foreach has thousands of results. But it is fine for small results.

Boss wants to view report, I would have a paginator kind of in a paginator. I.e., where inner foreach has a whole lot of results.

This is something you will never see in a video, I promise. WHY -- Large enterprise don't use active record on large database results, it breaks..

1 like
LaraBABA's avatar

Thank you both for your replies. I did not know about active records not being a good choice for large enterprises.

Please or to participate in this conversation.