Heads up: this article is over a year old. Some information might be out of date, as I don't always update older articles.


The Laravel documentation is great most of the time, but when you are stuck on a particular problem you have to figure by yourself what is the solution. Recently I had to create manually a paginator instance using the LengthAwarePaginator class. The first attempt was nearly a success, here a snippet of my controller:


$page = $request->has('page') ? $request->get('page') : 1;

$data = $this->userRepository->paginate($request->all(), $page);

$users = new LengthAwarePaginator($data->items, $data->totalItems, 15, $page);

Pretty straightforward! The LengthAwarePaginator class constructor accepts the items, the total number of items, the items per page and the current page. In the browser however I immediately noticed that, when switching from the first page to the second, all the query parameters got lost.

How do I solve this problem? By looking up into the Laravel API of course.

Turns out that the constructor of LengthAwarePaginator accepts an optional array as a fifth parameter formed by the following entries path, query, fragment, pageName.

So I needed to edit the code as the following:


$users = new LengthAwarePaginator($data->items, $data->totalItems, 15, $page, [
    'path'  => $request->url(),
    'query' => $request->query(),
comments powered by Disqus

Database Design for Bus Timetables


Recently, for a large website commissioned by the local agency for tourism, I had to design a system for storing …