KingsleyO's avatar

Appending array to pagination

I have this code to filter categories which is an array plus location.


    $category = Input::get('maincat', []);
   $locate = Input::get('location');

    
      $list = Buy::with('catbuy');

      

    foreach ($category as $k => $cat) {
    $list->whereHas('catbuy', function($q) use ($cat){
        $q->where('id', $cat);
    });
}


    $this['requests'] = $list->where('location', 'like', '%' . '' . $locate . '' . '%')->orderBy('created_at','desc')->paginate(1)->appends(['location' => $locate, $category] );


}

The above filters correctly and shows the correct number of pages in pagination, but clicking on page 2 or any other page resets it and the queries no longer work. Is there a way to rewrite the code so appends will work correctly?

The url of the of above when a second page is clicked looks like this

http://localhost/buy-request-filter?location=Kano&0%5B0%5D=7&0%5B1%5D=14&page=2

0 likes
3 replies
jlrdw's avatar

There is literally an example of that right in the documentation chapter on pagination.

KingsleyO's avatar

Paginating as in the documentation was not the issue. I maybe asked it wrong. well got it working using appends(Input::all() and rewriting the query to

$this['requests'] = $list
     ->whereHas('catbuy', function($q) use ($category){
        $q->whereIn('id', $category);
    })
    ->where('location', 'like', '%' . $locate  . '%')

    ->orderBy('created_at','desc')->paginate(1)->appends(Input::all());

Please or to participate in this conversation.