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

dmag's avatar
Level 6

Session item is removed after login

When a guest user puts items into a cart, they are stored in a session

session('cart', 'item1')

However once user has logged in, this session gets destroyed for some reason and I can no longer access it. Why does this session is removed and how do I keep it after user log in?

0 likes
7 replies
sitesense's avatar

A new session is created after login.

In your login function you could copy the cart from the session to a local variable and then after Auth::login($user), put the cart from the variable back into the session.

Something like:

$cart = Session::get('cart'');
Auth::login($user);
Session::put('cart', $cart);
1 like
dmag's avatar
Level 6

I tried to dd() the session even before the validation and login attempt takes place, but it still is removed.

public function postLogin(Request $request) {
    dd(session('cart'));
    
    // validate
    // attempt to log in
}

It seems that it gets removed after "post". Is it a normal behavior in Laravel 5 or I'm doing something wrong?

bashy's avatar

Tried reading it on another route?

dmag's avatar
Level 6

@bashy when I access the same method via get , it reads well, when I switch back to post the session item is removed. It seems that post request somehow removes this session item.

bashy's avatar

Have/can you test it on another route (not login) and do GET/POST requests and it stays/erases?

dmag's avatar
Level 6

@bashy it does stay when I tried it on another (not login) route, both GET/POST requests. But when I tried to post to postRegister method which is in the same controller, then the session erases just like in case of the postLogin.

Please or to participate in this conversation.