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

murilo's avatar
Level 10

Laravel Session Save is not working with Request

Hello , I am trying to save a session in Laravel with my Request . but it is not working .

this is the session -

  public function save_session(Request $request)
    {
          $request->session()->put('my_session', "it saved one value");
          $request->session()->save();
           return ['successs' => true ];
}

if I access the route of the session like this -

mywebsite.com/save_session

It will works good and save the session . BUT , if I send a request from VUE , like this -

            axios.get(/save_session)
                .then(response => {
                     console.log(response.data); // {successs: true}

                })

It will get my response , BUT WILL NOT SAVE THE SESSION . I dont know what is happen , If I access the route . It works the session . BUT if I send a request from VUE . IT not works .

0 likes
8 replies
murilo's avatar
Level 10

I added this code in the Kernel.php -

  protected $middleware = [
...
        \Illuminate\Session\Middleware\StartSession::class,
    ];


ALSO

'api' => [
    \Illuminate\Session\Middleware\StartSession::class, 
            'throttle:3000,1',
            'bindings',
        ] ,


BUT IT DIDNT WORK , it works if I access the url BUT does not work if I send a request from VUE

fylzero's avatar

@murilo You shouldn't have had to add \Illuminate\Session\Middleware\StartSession::class, to middleware, that is stock Laravel.

What are you using for SESSION_DRIVER in your env file? Try changing this to cookie.

Are you sure your route is a get in your web.php file?

How are you checking if the session is saving?

24 likes
Sinnbeck's avatar

How do you know it doesn't work? How do you test it?

murilo's avatar
Level 10

Hello @fylzero AND @sinnbeck , after many tries and testing , I start to understand what is happening .

well , In VUE if I load the page and just make this request -

this.$store.dispatch('save_session_search') ,

IT will works . but the problem is that I have other requests before this one to load the page , it is like this

 loadPage() {

    
                Promise.all([

                // RESET ALL FILTERS
                    this.$store.dispatch('reset_all_filters') ,

 
                    // EXTRA OPTIONS
                    this.$store.dispatch('load_extra_options') ,

                    // LOAD OPTIONS
                    this.$store.dispatch('load_form_options')

                ]).then((res) => {
                // THEN I SAVE THE SEARCH SESSION
                this.$store.dispatch('save_session_search') ,
         });

 });




LIKE THIS THE SESSION IS NOT WORKING . but it access the laravel page , I know that access there becose I tested in my console . but it not save the session .

My question is , does laravel limit the session , when make many requests ? I have never seen this .

murilo's avatar
Level 10

If I do like this ( with setTimeout ) , it will works the session . otherwise , It does not works .

loadPage() {

    
                Promise.all([

                // RESET ALL FILTERS
                    this.$store.dispatch('reset_all_filters') ,

 
                    // EXTRA OPTIONS
                    this.$store.dispatch('load_extra_options') ,

                    // LOAD OPTIONS
                    this.$store.dispatch('load_form_options')

                ]).then((res) => {
              

         let wait = setTimeout(() => {
                            clearTimeout(wait);
                            // THEN I SAVE THE SEARCH SESSION
                          this.$store.dispatch('save_session_search') ,
                        }, 6000)
         });

 });

murilo's avatar
Level 10

it will not work . I dont know why . I think that is becose it has many requests , in a short time , and the session does not work . maybe is that

Please or to participate in this conversation.