'Laravel resource collection, always pass request parameters

I'm working on a Laravel 7 project and I'm using API resource collections and pagination. I'm stuck at something. Users calling the API can simply pass two variables in the url. The first one is how many objects returned in each page and the second one is what page should be returned. For example:

/api/users?limit=5&page=2

means the API should return only five results per page and it should also return the second page. From the paginate function this can be done easily as:

$users->paginate(5,['*'], 'page_name', 2);

And I can simply read these values from the request and pass them to the paginate method. But I want this to be done automatically each time I call paginate() Since these parameters can be passed to any API and I don't want to repeat the code of reading these values and pass them to paginate() each time I call it. Is there a way to do that? Maybe with serviceProviders?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source