uccdev's avatar

Paginate a database's complete results?

Hi, I need help with paginating my database.

Basically, I want to do a complete query from my DB, and I want to chunk it off so that only certain sections of it are given at a time - the rest can be scrolled through via pagination.

e.g:

   $dairies = DB::table('dairies')
select('name', 'stock')
->get();

This would return every entry in the 'dairies' table, all 20,000+ of them.

I know of the "->paginate(num)" extension I can add to my DB::table, like so instead of 'get':

   ->paginate(50);

But what I need to do isn't simply to paginate the result. I need to spread it out so that, rather than load all 100,000+ entries in the DB, I only load, say, 50 at a time, and hitting the 'next' or 'previous' button will get the next batch of 50 after that.

Does anyone know how to do this? If you could spare the time, your help would be greatly appreciated!

1 like
11 replies
zion's avatar

You can use skip() and take() for this.

$dairies = Dairies::skip(50)->take(50);

This will skip the first 50. All you need to do now if calculate how many you need to skip each time. You can, for example, send a page variable from the frontend to the backend and use that to calculate the offset/skip.

thoasty's avatar

I know of the "->paginate(num)" extension I can add to my DB::table, like so instead of 'get':

only load, say, 50 at a time, and hitting the 'next' or 'previous' button will get the next batch of 50 after that

Thats exactly what paginate() (instead of get()) does.

uccdev's avatar

@ZION - Thank you very much, this looks to be on the money for what I want! I can't test it just yet since my server is currently down, but once it's up, I'll test it and update you

uccdev's avatar

@THOASTY - Indeed, but I need a step beyond that. I need to be able to go from the current 50 to the next 50, when the user presses the Next button, and vice versa for the Previous button. Any advice?

realrandyallen's avatar

@UCCDEV - I think that's what @thoasty is trying to tell you is that the pagination feature does exactly that. Also, when you click a new page (whether it be next or previous) you will get the previous 50 or next 50 automatically (Laravel Magic)...you don't need any extra logic in your controller or anywhere else.

1 like
uccdev's avatar

@REALRANDYALLEN - I see. I guess part of my problem is I don't have the buttons to cycle through paginations. I don't have the Next and Prev buttons to cycle through the amount of entries overall - how exactly should I set those up? The pagination documentation doesn't seem to cover them

uccdev's avatar

@VILFAGO - I'd like to try that, but when I try this:

           {{$dairies->links}}

I get this:

            "Undefined property: Illuminate\Pagination\Paginator::$links..."

I'm using Paginator in my namespace with the following line:

              "use Illuminate\Pagination\Paginator;"

But I still get the error

EDIT::: I should have used "links()" instead of just "links". That fixed the error and gave me what I wanted. Thank you!

1 like
Vilfago's avatar

Sorry, you're right. I edited my answer.

You can choose the best answer to close this thread. It could help someone

jlrdw's avatar

rather than load all 100,000+ entries in the DB, I only load, say, 50

When you paginate a query, it doesn't load all. It retrieves only 50 (or whatever number) at a time.

When you click next, for next page the query is run again to get next 50.

Please don't tell me you are attempting to put 100,000 in an array (collection) first.

Newbies get so confused on when to and when not to use a collection.

You should be well versed in how pagination works in mysql before even thinking about using laravel.

You actually have a user who is going to page through that?

It's easier to retrieve only the data needed, like have the database return only names that start with wh as example:

  • white
  • whitten
  • etc

You could perhaps gain from taking some general database tutorials.

Please or to participate in this conversation.