Laravel Multiple Pagination in One Page

Laravel Multiple Pagination in one page

Unfortunately I can't test this code right now, but browsing at the docs and the code (in Illuminate/Pagination/Environment) I guess you could something like this:

# use default 'page' for this
$collection1 = Model::paginate(20);

# use custom 'other_page' for this
$collection2 = Model2::paginate(20);
$collection2->setPageName('other_page');

The setPageName() method isn't documented in the docs, but it's a public method alongside those indicated in the documentation, so it should be working fine. FOr reference, this is the declaration (l. 171-180 in vendor/laravel/framework/src/Illuminate/Pagination/Environment.php):

/**
* Set the input page parameter name used by the paginator.
*
* @param string $pageName
* @return void
*/
public function setPageName($pageName)
{
$this->pageName = $pageName;
}

Now take into consideration that you will have another query string appended to the url, so you need to tell the pagination to consider it. Use the appends() method:

$collection1->appends(array_except(Request::query(), 'page'))->links();

$collection2->appends(array_except(Request::query(), 'other_page'))->links();

That is, tell each Presenter to build up the url with all the query strings (the array resulting from Request::query() without the current index used by the paginator, or you'll end up with a double value). array_except() is a Laravel built in array helper that returns the given array (1st parameter) purged of the passed index (2nd parameter).

I'll try to test this code as soon as I can, but it should work. Let me know in any case!

Laravel 6 multiple pagination in one page

This should be possible as long as you keep track of the page value for the opposite set:

{{$infos->appends(['info' => $infos->currentPage(), 'berita' => $beritas->currentPage()])->links()}}

{{$beritas->appends(['berita' => $beritas->currentPage(), 'info' => $infos->currentPage()])->links()}}

I'll try and test this theory out later if I find time.

How to implement multiple pagination in single page | laravel 5.7

Yes its because by default pagination url with page

To Change just pass few extra parametes to paginate method

FROM

$products=Product::paginate(10);
$users=Users::paginate(10);
return view('index',['products' => $products, 'users' => $users ]);

TO

$products=Product::paginate(20,['*'],'productPagination');
$users=Users::paginate(20,['*'],'userPagination');
return view('index',['products' => $products, 'users' => $users ]);

Laravel 5 multiple pagination in a single page

If you see in the laravel core, then you will see paginate function something like this

/**
* Paginate the given query.
*
* @param int $perPage
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*
* @throws \InvalidArgumentException
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
$total = $this->query->getCountForPagination();
$this->query->forPage(
$page = $page ?: Paginator::resolveCurrentPage($pageName),
$perPage = $perPage ?: $this->model->getPerPage()
);
return new LengthAwarePaginator($this->get($columns), $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
}

Now in above function you can pass $pagename to make both pagination working separately like below

$collectionA = ModelA::paginate(4, ['*'], 'pagination_a');
$collectionB = ModelB::paginate(4, ['*'], 'pagination_b');

Or You can also use setPageName function for this

Multiple Laravel 5 Paginations within the same page

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.



Related Topics



Leave a reply



Submit