4

I'm trying to have 2 paginations on a single page.

View:

{{!! $items->appends(['page2' => Request::input('page2', 1)])->render() !!}}

But it is not working, as using custom $pageName for pagination ($items->setPageName('custom_page_parameter')) links are not working in laravel 5.

https://stackoverflow.com/questions/29035006/laravel-5-pagination-wont-move-through-pages-with-custom-page-name

Here is how I did it in laravel 4:

Laravel Multiple Pagination in one page

What is Laravel 5 way of doing this?

Community
  • 1
  • 1
leoel
  • 307
  • 5
  • 17

4 Answers4

11

I've spent far too much time trying to find out how to do this, so thought I'd share my findings in case there is another poor soul like me out there. This works in 5.1...

In controller:

$newEvents = Events::where('event_date', '>=', Carbon::now()->startOfDay())
    ->orderBy('event_date')
    ->paginate(15,['*'], 'newEvents');

$oldEvents = Events::where('event_date', '<', Carbon::now()->startOfDay())
    ->orderBy('event_date', 'desc')
    ->paginate(15,['*'], 'oldEvents');

Then continue as usual in the view: `

// some code to display $newEvents
{!! $newEvents->render() !!}

// some code to display $oldEvents
{!! $oldEvents->render() !!}

Now, what this doesn't do is remember the page of $oldEvents when paging through $newEvents. Any idea on this?

References:

Timothy Victor
  • 140
  • 2
  • 7
  • In short, ->paginate(15, ['*'], 'newEvents'); Do not forget the missing quote around 'newEvents' – Snook Jun 01 '16 at 18:57
  • In order to remember the page number for $oldEvents when paging $newEvents, use this: {{ $newEvents -> appends(["oldEvents" => $oldEvents -> currentPage()]) -> links() }} and vice versa for paging oldEvents: {{ $oldEvents -> appends(["newEvents" => $newEvents -> currentPage()]) -> links() }} – John1984 Oct 15 '16 at 10:56
7
$published = Article::paginate(10, ['*'], 'pubArticles');
$unpublished = Article::paginate(10, ['*'], 'unpubArticles');

The third argument for paginate() method is used in the URI as follows:

laravel/public/articles?pubArticles=3

laravel/public/articles?unpubArticles=1

anonym
  • 4,460
  • 12
  • 45
  • 75
5

In Controller:

$produk = Produk::paginate(5, ['*'], 'produk');
$region = Region::paginate(5, ['*'], 'region');

in view:

{{$produk->appends(['region' => $region->currentPage()])->links()}}    
{{$region->appends(['produk' => $produk->currentPage()])->links()}}   

reference to :[Laravel 5] Multi Paginate in Single Page

mosleim
  • 654
  • 6
  • 19
0

Try using the \Paginator::setPageName('foo'); function befor buidling your paginator object:

    \Paginator::setPageName('foo');
    $models = Model::paginate(1);
    return view('view_foo', compact('models'));

This question might help too: Laravel 5 pagination, won't move through pages with custom page name

Also note there is a bug atm: https://github.com/laravel/framework/issues/8000

Community
  • 1
  • 1