Sam Chahine
Sam Chahine

Reputation: 630

Multiple Laravel 5 Paginations within the same page

I have a container that I want to fill with items from my database, say every row has these columns:

$item->title
$item->description
$item->color
$item->id

I have a main-container where I want to loop through all of my items, but only 5 at a time. I know I can have all of them, but I really want there to be no scrolling.

I know for a whole page you would just replace ->get(); with ->paginate(x); where x is the number of items you want to show per page.

But I also have a secondary-container where I want to loop through a bunch of other items, but I wouldn't know how to implement the second one, since the main-container would work when the url extends ?page=x where x is the nth chunk of items iterated.

Would I be able to have a custom get url request (not sure about the term, correct me if I'm wrong) and do something like ?page2=x for my secondary-container?

I searched arround but didn't quite know how to put my question into a small answer, and the results weren't helpful. Thank you in advance.

Upvotes: 0

Views: 1443

Answers (1)

jszobody
jszobody

Reputation: 28941

If you look at the paginate method:

https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Builder.php#L429

You see that it accepts a $pageName variable, which tells it which request variable to use.

So you can do this:

MyModel::where(...)->paginate(20, ['*'], 'fooPage');

And Laravel will use $request->fooPage for the page variable.

Or, you can see the paginate method also allows you to just explicitly pass in the page variable itself as the fourth parameter:

MyModel::where(...)->paginate(20, ['*'], null, $request->myPageVar);

And now a GET variable ?myPageVar=1 will set the page.

Upvotes: 3

Related Questions