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

the-ginga-ninja's avatar

AJAX and Session Persistence not working

Hi Laracastians, I'm working on building a little compare bin and I'm having an issue with persisting values to session storage with AJAX requests. Here is my method in my controller

public function postAddPersistence() {
        $postData = \Request::all();

        $pattern_compare_persistence = \Session::pull('pattern_compare_persistence', []);

        $already_in_array = array_search( $postData['pattern_slug'], $pattern_compare_persistence, true );

        if($already_in_array === false) {
            $pattern_compare_persistence[] = $postData['pattern_slug'];
        }

        \Session::put('pattern_compare_persistence', $pattern_compare_persistence);

        \Session::save();

        return \Response::json(\Session::get('pattern_compare_persistence'));
}

And here is my AJAX call (I'm using vue.js)

if(add_to_persistence) {
    var postData = {pattern_slug: pattern_slug};
    this.$http.post('/api/patterns/add-persistence', postData, function() {}, function(error) {});
}

Now, the issue is that things seem to be working intermittently. If I print_r(\Session::all()); right before the return statement, I will see the 'pattern_compare_persistence' key with the newly added pattern_slug correctly in the array. But when I check the persistence, the newly added pattern_slug is not actually in the array.

What is breaking my brain a little bit is the fact that when I do a test via the Postman chrome extension, the values will be added to the session persistence perfectly. Am I doing my AJAX call incorrectly? Am I not returning correctly in order for the Session to save properly?

0 likes
4 replies
J_shelfwood's avatar

Are the headers and such the same with both calls? Compare postman's request data with the one in vue js (network tab in dev tools)

I'm struggling with understanding it as well, this should probably shed some light on why it's not working the way you expect it to though.

Please or to participate in this conversation.