Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tobe81cwb's avatar

Laravel 5 set current page programatically

Trying to upgrade my app from Laravel 4.2 to Laravel 5.0

My web application uses a lot of ajax, then the pagination has to be a little more dynamic, without relying on urls, but by request and other items which are not fitting in this conversation.

In laravel 4.2 I could to set the current page, programmatically, by simply calling the function: Paginator::setCurrentPage($currentPage);

In Laravel 5.0, this is no longer possible. Is there a practical and fast way to do the same in Laravel 5.0?

Thanks.

0 likes
5 replies
tobe81cwb's avatar

Or maybe if someone can show me an example of paging in ajax working with Laravel 5.0 (items are inside a div). With Laravel 4.2 that was easy to do, because I had more control over pagination.

I even tried to make a workaround, by setting the page in $ _POST or $ _GET, but without success in Laravel 5.0 (though bad, it worked in Laravel 4.2). Another possibility is if I do fully manual paging directly on repositories, including pagination options, however, will have to modify multiple code snippets.

Another solution I found just now, without making ugly hacks, and that may not change much in the programming made so far, is to define a currentPageResolver on Paginator. This is not mentioned in any time in the Laravel documentation, so it took me a while to find it. Founded this option looking in github and source codes.

tobe81cwb's avatar
tobe81cwb
OP
Best Answer
Level 2

Found a simple solution:

Paginator::currentPageResolver(function() use ($currentPage) {
    return $currentPage;
});
4 likes
laloutre's avatar

Your solution isn't working with Laravel 5, right? I think it was working when Paginator was a facade, but this isn't the case anymore... Or am I missing something?

I'm looking for a simple way to force the page before a paginate() eloquent call.

tobe81cwb's avatar

See the "best answer" solution! With this piece of code, I can change currently page programatically. Works OK on L5.

Something like this (not tested):

$currentPage = 5;
$totalPerPage = 10;

// force current page to 5
Paginator::currentPageResolver(function() use ($currentPage) {
    return $currentPage;
});

// list all users, with 10 users per page, on page 5
$users = User::all()->paginate($totalPerPage);
3 likes
thiduzz's avatar

Hey Tobe, nice to see some brazilian faces around here (and also from CWB). Anyway, your solution worked well for me, but to make it work I needed to put the Paginator::currentPageResolver block above to the Eloquent paginate call.

1 like

Please or to participate in this conversation.