gabahulk's avatar

Pagination of array not working

Hello! I need to paginate an array of data and the render method works, the url is correct but they always show the same items!

 $items= new Paginator($items,20);
  return View::make('transactions.backpack')->with('items', $items);

I tried to use the array_slice method but then the pagination does't work (because it's always the last page)

I am almost sure that I am not using the paginator class correctly, but I really didn't find a place that explains how to use it with an array (without Eloquent).

Thanks!

0 likes
2 replies
uxweb's avatar

@gabahulk I think that with any manual pagination you should slice the items to show according to the current page.

$perPage = 20;
$curPage = Paginator::resolveCurrentPage();
$offset  = ($curPage * $perPage) - $perPage;

$items = new Paginator(array_slice($items, $offset, $perPage), $perPage);

return View::make('transactions.backpack')->with('items', $items);
gabahulk's avatar

@uxweb Thank you for the answer! But I am afraid I already did that. The problem of slicing the array first is that when I pass it to the view it has the flag "hasMore" false (because the sliced array really has no more items) . Also, how can I get the

Paginator::resolveCurrentPage(); 

if there is no instance of Paginator to this point?

Thanks!

Please or to participate in this conversation.