How to customize the Laravel pagination parameter name. Very often I have to create APIs for other development teams. And one of the most common tasks is the pagination of data. Thanks to such tasks, I learned a lot about pagination in Laravel: how to override a key page with a custom one, or even how to specify a specific page to display.
And in this article, I will tell you how to set the parameter that is responsible for the page number in Laravel pagination.
This code returns us 5 records, and additional metadata about the number of records, pages, links.
Very often we have to send this metadata to the frontend. And if we examine the structure of the paginator’s response in more detail, we will notice that the parameter next_page_url points to the next page of the selection results. And the information about the page number is set by the parameter page.
And this functionality is enough for any standard project. However, there are times when we still need to override the parameter page in the paginator with our own. For example, when several paginations are added on one page. Because if you go to the next page of one paginator, it would also affect the second.
Table of Contents
How to change the “page” query parameter
There are two ways to do this:
- Pass a custom name to the method paginate.
- Use the paginator method setPageName.
Let’s pass the custom parameter name to the method paginate. Yes, many don’t know, but the method paginate takes 4 arguments:
- A number of records per page (limit).
- Table fields to return.
- Page number key (by default, page ). This is what interests us.
- Pagination page number. You can set the page number manually, however, you just need to redefine the key, and the page number will be read automatically using the key from the request.
In the current version, the default parameters look like this:
Order::paginate(5, [‘*’], ‘page’);
The second argument is an array of table columns that you want to return in the response, by default this *is what returns all fields.
However, our interest lies in the third argument page, where you can specify the desired key name. We will name it orders_page.
Order::paginate(5, [‘*’], ‘orders_page’);
Now, if we refresh the page, we will see that everything works, the metadata of the paginator has changed, and the pagination link now contains a parameter orders_page.
Use the method setPageName to output the results to the page. This method is mostly described in order to explain how it works, and why, it does not quite fit the current task.
$orders = Order::paginate(5);
And the result will be:
However, this method does not work as many of you might think. Bypassing the parameter, setPageNameyou may wonder why pagination is not working. And the answer is really simple.
In fact, the method setPageName does not affect the database query in any way. It only changes the page number parameter in the metadata.
Because we first get the result with standard parameters:
`page` $orders = Order::paginate(5); //
And after that, we call the method setPageName on the collection of results already received from the database. With this method, we simply replace the parameter in the paginator link, no longer being able to influence the formation of the request, since it has already been executed by the method paginate.
As they say, it’s too late, mister, to drink Borjomi if a request to the database has already been made.
In this article, we looked at how to work with the Laravel paginator, and how to pass custom parameters for the request and meta information. An example of Laravel pagination was given by changing the request key and passing the page number directly.
From this, you should have understood that the method setPageNameis a paginator method, not a QueryBuilder. Therefore, it only affects the paginator’s metadata and not the request when it is received.
Also, this article is very useful for those who did not know how to place several Laravel paginators on one page. This is done simply by specifying a unique page number key for each one.