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

Lowelheim's avatar

Session forget, flush, pull... do not work

I assume it's me, because I am formerly a .NET dev and this is only the third web app I'm dev using Laravel.

Said that, I basically used the global session to store a simple parameter necessary in a couple of views and I set to remove this when the user returns to the home page of the web app, the instruction is within the controller.

So far nothing worked, the parameter stored in the session lasts until, finally, I close the browser and this only thank to config/session.php, expire_on_close set to true.

I use the database as driver to store the session and the user that is browsing and use this parameter is not authenticated.

Do I miss something?

Kind regards

0 likes
5 replies
Snapey's avatar
Snapey
Best Answer
Level 122

when working with the session, one important rule is to allow the request cycle complete normally as the updating of the session storage is only performed at the end of the request. So if you dd() to check session, you actually prevent the session being updated.

Laravel Debugbar is good for examining session values

Apart from that, as long as your session key is accurate you should not have any problems

1 like
Lowelheim's avatar

@Snapey first, thank you for your courtesy and patience.

I was aware to avoid dd() as debug method for everything related the cycle of the request and the session, instead I was not aware of Laravel Debugbar and I immediately installed it, this will add value to my debugging.

I will keep investigating using the newly addon of Debugbar and update if I eventually discover my error.

Lowelheim's avatar

@Snapey ok, surely the value of Laravel Debugbar was immense.

I finally get the error I made, was in the web.php, fixed, now all works like a charm.

Kind regards.

LaryAI's avatar
Level 58

It sounds like you're having trouble with managing session data in Laravel. Here are a few steps and code examples to ensure that your session data is being managed correctly, especially when trying to forget or flush session data.

  1. Ensure Session Middleware is Applied: Make sure that your routes are running through the session middleware. This is crucial because Laravel's session operations depend on the session middleware to start and manage sessions.

  2. Using Session Correctly in Controller: When you want to forget a session key or flush the entire session, you should ensure that you're using the session global helper or request instance correctly.

Here's how you can forget a single key, flush the entire session, or pull data from the session:

public function home(Request $request)
{
    // Forget a single key
    $request->session()->forget('key');

    // Alternatively using global session helper
    session()->forget('key');

    // Flush the entire session
    $request->session()->flush();

    // Pull data from the session and then forget the key
    $value = $request->session()->pull('key', 'default');
    
    return view('home');
}
  1. Configuring Session Driver: Since you are using the database as a session driver, ensure that your database table for sessions (sessions table) is correctly set up. You can create this table by running the session table migration that comes with Laravel:
php artisan session:table
php artisan migrate
  1. Session Lifetime: Check the config/session.php for the lifetime setting to ensure sessions last as long as you expect them to. Since you mentioned expire_on_close, make sure this is behaving as expected. This setting should be true if you want sessions to expire when the browser closes.

  2. Debugging Session Issues: If the above steps don't resolve the issue, you might want to add logging to see if and when the session keys are being set, accessed, and removed. You can log session operations like this:

\Log::info('Session Data:', $request->session()->all());
  1. Clear Config Cache: Sometimes changes in config files are not picked up because of configuration caching. You can clear the config cache by running:
php artisan config:clear

Try these steps and see if the issue with session management resolves. If the problem persists, it might be helpful to check for any middleware that could be interfering with session data or any other part of your Laravel application that might be misconfigured.

Please or to participate in this conversation.