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

ssquare's avatar

Session Variable disappeared in controller

I am trying to store booking details using a session variable. In the first function:

public function hold(Request $request){
         $details = [
                'times' => $arr,
                'subTotal' => $subTotal,
                'tax' => $tax
            ];
            $request->session()->forget('cart');
            $request->session()->put('cart',$details);
}

Next, it will be posted to another function of same controller. Upto this point session is available.

public function checkout(){
    dd(session()->all());
}

Next route is on payment controller. This is another controller than the previous one. Here, the session variable is empty.

public function createPayment(){
    dd(session()->all());
}

What am I doing wrong? How can I access session in this createPayment function?

0 likes
3 replies
siangboon's avatar

i think it may be better to use

session(['key' => 'value']);

to assign instead to assign into $request->session().

ssquare's avatar

@SIANGBOON - I have just tried as your mentioned method, but still, it is returning an empty array.

Snapey's avatar

bear in mind that session data is only written to the session at the end of the request cycle.

If you dd() in your code, this save function never runs.

You can explicitly save using session()->save()

It might be better to switch to using the dump server and then dump() in your code not dd()

Laravel Debugbar is also very useful to have installed when wor king with session data

3 likes

Please or to participate in this conversation.