Reputation: 4285
I've got another pagination problem it looks like. When I iterate over the paginated array below, I get the entire array every page.
$array = [...];
$ret = new LengthAwarePaginator($array, count($array), 10);
// dd($ret);
LengthAwarePaginator {#302 ▼
#total: 97
#lastPage: 10
#items: Collection {#201 ▼
#items: array:97 [▶]
}
#perPage: 10
#currentPage: 1
#path: "/"
#query: []
#fragment: null
#pageName: "page"
}
This isn't the case when building a LAP from an eloquent model eg: Blah::paginate()
Upvotes: 1
Views: 8136
Reputation: 219946
The paginator does not slice the given array for you automatically. You have to slice it yourself before you pass it to the paginator.
To make life easier for you, use the collect
helper to create an instance of a laravel collection, which makes it very easy to slice up:
$items = collect([...]);
$page = Input::get('page', 1);
$perPage = 10;
$paginator = new LengthAwarePaginator(
$items->forPage($page, $perPage), $items->count(), $perPage, $page
);
Upvotes: 19
Reputation: 4285
The LengthAwarePaginator does not chunk autmoatically.
An quick fix is to do something similar:
foreach($col->slice($col->perPage() * ($col->currentPage() - 1), $col->perPage()) as $item)
{
// do blah
}
Upvotes: 0