user10259049
user10259049

Reputation:

set current page in laravel 6 paginate without ?page=2

I need so set current page in laravel pagination without get page number from get parameter.

the thing some like this:

$fetch = Post::paginate()->setCurrentPage(3);

It's possible to do?

Upvotes: 1

Views: 2471

Answers (2)

namal
namal

Reputation: 1274

This should work

$posts = Post::where('status', 1);
$posts_temp = $posts->paginate(2);

if(!$request->has('page')) {
  Paginator::currentPageResolver(fn() => $posts_temp->lastPage());
}

$posts = $posts->paginate(2);

Upvotes: 0

Dan
Dan

Reputation: 5358

Yes, it is possible to override the Paginator::currentPageResolver() to change the way the page is determined:

Paginator::currentPageResolver(fn() => 10);

$users = User::paginate(); // you now will be on page 10

Unfortunately there is no way to reset or retrieve the original function set in the PaginationServiceProvider as the property is protected and there's no getter.

Upvotes: 2

Related Questions