layer7's avatar
Level 24

Paginator returns all items instead of subset

Hi all,

I'm testing out Laravel 5 and have run into an issue where pagination doesn't work when I'm manually instantiating the paginator. Here's some code:

 /**
  * Display a listing of the resource.
  * @Get("/users")
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
    $allUsers = $this->userRepository->all();
    $paginator = new LengthAwarePaginator($allUsers,count($allUsers),15);
    $users = $paginator->items();
    $links = $paginator->render();
    return view('users.index', compact('users','links'));
 }

Noteworthy perhaps is that this happens with the regular Paginator class as well as with the LenghtAwarePaginator. I tried with both User::all() and through the repository. The end result is that my view displays all records returned in the table I display in the view, but the pagination links do show 3 pages (I have 41 records in the table and with the 15 per page I am requesting, I'm expecting 3 pages total). Also the links are pointing to the wrong URL, but I figure that's a different problem which I haven't investigated yet.

Has anybody else had this experience? Am I using it wrong? Any input is greatly welcomed.

/Koen

0 likes
0 replies

Please or to participate in this conversation.