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

bortz's avatar
Level 1

Laravel session variable disappears after a while

Hello there, I declare a session variable upon successful login, by doing something like:

session(["myvar" => "something"]);

in LoginController.

The problem is that after a while (session not yet expired), this variable suddenly gets unset. Do you know why?

0 likes
9 replies
Snapey's avatar

Is the user still logged in?

1 like
jlrdw's avatar

@bortz when you loose myvar in session, do you also loose the current login. Try an Auth::check:

if (Auth::check()) {
    echo "The user is logged in";
}
1 like
jlrdw's avatar

@bortz your myvar then should still be available. Check for syntax errors. And show where you are using it where it's not available all of a sudden.

Edit:

I normally use the facade:

Session::put('search', $search); 

But the helper should work the exact same.

2 likes
bortz's avatar
Level 1

@jlrdw No errors. The variable is just empty if I show it with dd. Now I'm using the facade too. I will update you about it.

FSElias's avatar

Hi, getting the same issue here In Laravel 8, the user is still logged in yet the session value is gone after a while

Please or to participate in this conversation.