ondes's avatar
Level 1

Multiple custom paginations & pagination links problem

Hello everyone!

I desperately need help with some issue with i have, concerning multiple paginations (2 paginations) on one page and links of pagination are not working properly.

Situation is following: I got eloquent object- i sort it and do some process, then i converit it to 2 separate collections- now i have two collections which i have transfered by "Lengthawarepaginator"- and added pagination name for each by method "setPageName()". (!!! I HAVE to do this by custom paginate, since i am working with those, so more specific eloquent query is out of question)

Everything is working perfectly- when i click on any paginate link of any of those 2 paginations- it shows proper paginated results for that pagination, simply perfect.

BUT- pagination links are not working in proper display- i.e. when i click on page 2,3,4 whatever- even i get displayed proper page of pagination- pagination link stays ALWAYS at number one- appearing like it is displaying page one of pagination (even displaying f.e. 2,3,4 whatever pagination page results)- simply- the pagination links dont follow appropriate pagination page shown. Plus, link for page 1 of pagination is not clickable at all.

Here is my controller code:

$currentPage=LengthAwarePaginator::resolveCurrentPage('popular')-1;
        $perPage=10;
        $currentPageBlogResults = $items1->slice($currentPage * $perPage, $perPage)->all();
        $items1= new LengthAwarePaginator($currentPageBlogResults, count($items1), $perPage);
        $items1->setPath('offers');
        $items1->setPageName('popular');


        $currentPage=LengthAwarePaginator::resolveCurrentPage('highlow')-1;
        $perPage=10;
        $currentPageBlogResults = $items2->slice($currentPage * $perPage, $perPage)->all();
        $items2= new LengthAwarePaginator($currentPageBlogResults, count($items2), $perPage);
        $items2->setPath('offers');
        $items2->setPageName('highlow');

For the view: I simply foreach the results of each and under each i simply echo pagination links like: {{$items1->links() }} and {{$items2->links () }}

Any suggestions? Im running desparate, since i see i have something missing and for past 24 hours i was just trying to sort this issue out, plus i am being pushed by deadline to deliver.

Any ideas highly appreciated and I am highly thankful for it.

Carpe diem.

0 likes
6 replies
jlrdw's avatar

You probably need two custom lengthawarepaginator's. Or you could paginate in two separate divisions with some Ajax.

Edit I was on cell, ds now. I did a guide a while back:

https://laracasts.com/discuss/channels/tips/length-aware-paginator

another article

https://christophersax.com/2016/custom-lengthawarepaginator-in-laravel/

Naming (page) has to be different in each paginator, have one page the other p or pg for example.

I did a quick (no formatted) example a while back

https://drive.google.com/file/d/0B1_PFw--3o74YVliNHAxbzd4Z2c/view?usp=sharing

ondes's avatar
Level 1

Thank you for fast reply and all what you send. What you suggest to change in my code above to make it two custom lengthawarepaginators? Arent they two? (* code above i did based on one tutorial before)

ondes's avatar
Level 1

Everthing is running properly, just the links of the custom paginations in the view are not working- when you choose f.e. page 2, 3, whatever- you get your proper page displayed (for both two custom paginators on one page),even page url is fine, but the links simply always stays at page 1...with page link 1 not clickable.

this: http://i.prntscr.com/EoGHTeY1TsCsf-CVRVMvew.png

jlrdw's avatar

When you hit another page in the paginator, you need to get in the controller the state (current pages of each paginator) and update everything each time. And you need to have a different page name for one of the paginators.

Drop that slice junk, just paginate the queries normal.

See both links I gave, the lengthawarepaginator can be customized many ways.

In the link I gave see where I added more detail.

And in view

<?php echo str_replace('/?', '?', $dogs->appends($params)->render()); ?>

appends($params) if appending params.

Edit a while back @Snapey posted this:

http://novate.co.uk/using-multiple-pagination-links-on-one-page/

So the third argument in lengthawarepaginator

string $pageName = 'page'

is what you need.

In other words

My custom class can be modified as needed to suit your needs, my code is just examples.

Note, you have to go into the API and study what's going on: https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_paginate

Look up your laravel version.

1 like
ondes's avatar
Level 1

Thx a lot, probably easiest thing to me would be switching from collections to eloquent and sort it there....

Please or to participate in this conversation.