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

nnnayeem's avatar

Laravel Session not working

Hello everyone,

I am using laravel 6 and trying to use session and using database as a driver. But every time I am trying to it turns out to be a failure.

My session configaration:

SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_DOMAIN=null
CONNECTION=null

I am trying to save data in the following way:

session()->put('test','sdfsdfs');

I even used:

session->save()

I also tried cookie but it failed. I used dd() to check and it temporarily saves data. But later found out dumping data will not save to session. But even without dumping data, it's not working.

Then I found out in another discussion about changing the sessionStart middleware to the protected $middleware variable but it fails too.

Another thing is I tried dumping session() and saw there are data that is saved by the application and that means session is working. But for some reason I am not able to save the data.

Please give me some suggestion.

0 likes
3 replies
Snapey's avatar

laravel debugbar is a handy tool to have because it shows you what is in session (although in your case you can inspect the database)

So, are records being created and updated in the sessions table?

nnnayeem's avatar

@snapey thanks for your reply. I have found my problem. I was sharing my session data using view composer. For some reason this did not work:

$c = session()->get('cart.items');
View::composer('include.front_1_nav', function ($view)  use($c){
           $view->with('cartCount',count($c));
        });

If I dump there shows null. But if I dump in the view I see the session data. Currently I am doing this in a service provider boot method in the following way:

View::composer('include.front_1_nav', function ($view) {
            $c = session()->get('cart.items');
            if(empty($c))
                $view->with('cartCount',0);
            else
                $view->with('cartCount',count($c));
        });

I was thinking if I can increase only cart session lifetime without affecting other sessions.

Snapey's avatar

its probably the boot order of service providers meaning you are trying to get session data before it has been prepared.

Nothing whatsoever to do with session lifetime

Please or to participate in this conversation.