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

Deekshith's avatar

laravel pagination go to specific page which has particular post id

I have forum application where every thread will have posts so now i am storing last viewed page in database and if user again visits the same thread then i should redirect to that particular page. how can i do this? now i am using below code to redirect to last page by default,

f($request->page == '') {
                $lastPage = $thread->posts()->where('first_post',0)->orderBy('created_at', 'asc')->paginate(10)->lastPage();
                Paginator::currentPageResolver(function() use ($lastPage) {
                    return $lastPage;
                });
            }
0 likes
1 reply
a4ashraf's avatar

@deekshith

you need to override the Paginator::currentPageResolver() to current page ID

you will override this function in PaginationServiceProvider as the property is protected and there's no getter.

Paginator::currentPageResolver(10);

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


public function register()
    {
        Paginator::viewFactoryResolver(function () {
            return $this->app['view'];
        });

        Paginator::currentPathResolver(function () {
            return $this->app['request']->url();
        });

        Paginator::currentPageResolver(function ($pageName = 'page') {
            $page = $this->app['request']->input($pageName);

            if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) {
                return (int) $page;
            }

            return 1;
        });

        Paginator::queryStringResolver(function () {
            return $this->app['request']->query();
        });
    }


https://stackoverflow.com/questions/60901176/set-current-page-in-laravel-6-paginate-without-page-2

Please or to participate in this conversation.