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

kaiden's avatar

get pagination value from a session

making pagination with laravel is easy. however when you begin to modify somethings things get tangled. i want to get pagination value from a session not from post data via URL. well, i did not tried anything yet because i couldnt set up a roadmap. i know how to put a session and how to retrieve it but how i am gonna embed it into links() method?

Thats how my simple pagination works right now.

{{ $results->links() }}

EDIT: okey, i've found these two methods inside Illuminate\Pagination\AbstractPagination.php

/**
     * Resolve the current page or return the default value.
     *
     * @param  string  $pageName
     * @param  int  $default
     * @return int
     */
    public static function resolveCurrentPage($pageName = 'page', $default = 1)
    {
        if (isset(static::$currentPageResolver)) {
            return call_user_func(static::$currentPageResolver, $pageName);
        }

        return $default;
    }

    /**
     * Set the current page resolver callback.
     *
     * @param  \Closure  $resolver
     * @return void
     */
    public static function currentPageResolver(Closure $resolver)
    {
        static::$currentPageResolver = $resolver;
    }

as far as undersood these two methods decides which page we are on(because we can see it in the Illuminate\Pagination\LengthAwarePaginator.phps __construct method). However i am having a hard time since idk about whats a Closure is. also there is no $resolver property or method in the page. Is it possible if he's(Taylor guy) saying write a resolver() method inside the page if you wanna put page number by yourself?

0 likes
14 replies
Snapey's avatar

You would have to write your own paginator for that.

Cronix's avatar

however when you begin to modify somethings things get tangled.

Maybe try explaining what you're trying to modify? There may be a way to do what you're wanting without sessions, but it depends on what you're actually trying to do.

kaiden's avatar

@cronix well, there is a view that comes with every page. It has pagination. I want to send page number via sessions to one view to another because otherwise, you know.... how i can get page number with every view in the website? I dont want it to begin from page 1 again and again. Thanks

Cronix's avatar

In the controller where you are doing the pagination, like the user clicks page 1, just grab the page number from the $request and store that "last_page" in session?

kaiden's avatar

@cronix that will load the correct data from database. lets say user viewing page 3. and when he goes into a new view paginate will load correct data however!!!!, if i am not wrong,. $results->links() wil say to user that he is seeing 1st page even tho. he is on the 3rd.

Cronix's avatar

Something sounds very wrong about your approach, but if you need it to work that way, you'd have to create your own pagination and use sessions. It wasn't meant to do anything like this and this sounds very unconventional.

kaiden's avatar
kaiden
OP
Best Answer
Level 1

i did it laravel-fellas!!!

In order to this method to work you have to give pagination a new $pageName different than page. Luckly you can do that while calling your model like that

Model::paginate(10, ['*'], 'navi' ) //navi is the name of this paginator

and then go to the Illuminate/Pagination/PaginationserviceProvider.php and modify Paginator::currentPageResolver method like that.

Paginator::currentPageResolver(function ($pageName = 'page'){

            $page = $this->app['request']->input($pageName);
            

            
                //this if-else statements get $page variable from a session instead of url
                if( $pageName == 'navi' ){

                    if( filter_var($page, FILTER_VALIDATE_INT) !== false ){
                        
                        Session::put('currentPage', $page );

                        return (int) $page; 
                    }
                    else {

                        return (int) Session::get('currentPage', 1);
                    }
                
                }

            

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

            return 1;
        });

This ifelse statements will get $page variable from url if it has been set and will put it into a session. if no information comes with Request, $page variable will be get from session.

Yes i am proud.

Snapey's avatar

I can see that you are pleased with your work, but I have some bad news for you.

You should NEVER edit the code in the vendor folder.

Next time you upgrade, your code might be replaced, pagination will no longer work and you wont recall what you changed to make it work.

Cronix's avatar

I'm glad that you got it working, but you also kinda screwed yourself by altering a /vendor file. That means every time you upgrade laravel, you'll have to go redo your changes. You should never alter files in /vendor, unless you never plan on upgrading things.

It would be MUCH better to copy that service provider into /app/providers and alter it there.

Then edit /config/app.php, look in the $providers array and replace the default with your custom one. Then nothing will be overridden during upgrades and it will just use yours instead of the default.

'providers' => [
// ...long list of providers...

//Illuminate\Pagination\PaginationServiceProvider::class,
App\Providers\PaginationServiceProvider::class,
jlrdw's avatar

If messing with the vendor folder to modify pagination you really need to go back and take 6 months of basic PHP and MySQL before even messing around with laravel.

Custom pagination has got to be by far among the top five easiest things in the world to do.

kaiden's avatar

@jlrdw what is the meaning to use a framework if i am gonna make my own custom pagination, custom authentication, and then custom something else.

jlrdw's avatar

i want to get pagination value from a session not from post data via URL

First pagination comes from GET not POST.

Second the length aware pagination is easy to work with as I already said. I even did a simple guide on it.

https://laracasts.com/discuss/channels/tips/length-aware-paginator

and another article on it

https://christophersax.com/2016/custom-lengthawarepaginator-in-laravel/

And here

https://laracasts.com/discuss/channels/guides/getpdo-usage

I even show custom pager code.

Okay you want to store a page in session, fine. But normally it's done in the url.

With the build in paginator that works so good why would you want the page in session during pagination?

I can see storing the url and uri in session to return to a page you left off on after an edit, I do that all the time. But that has nothing to do with the actual pagination.

Snapey's avatar

what is the meaning to use a framework if i am gonna make my own custom pagination, custom authentication, and then custom something else.

ok, then use the paginator as it was designed?

Whats the advantage of using session for the page number?

Please or to participate in this conversation.