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

Teleogrid's avatar

Session not beeing saved in database

Hi people,

I'm trying to use the database driver to let Laravel save my session to a database table. I did the following:

// successfully created session table
php artisan session:table

// changed driver in session.php
'driver' => env('SESSION_DRIVER', 'database')

// changed driver in .env
SESSION_DRIVER=database

// migrate new database structure
php artisan migrate

Still: each time I refresh the page there is no session data saved in my database table. If I display my session id in the browser I get the same ID between pages. So there is an active session.

Another thing that is not working once I set the driver to database is the registration. User registration works when I use the dirver=file. But as soon as I change driver to database the registration page gives me a 419 Error

What's going on here?

0 likes
6 replies
himanshurajvanshi's avatar

Make sure are you use

$request->session()->put('key', 'value');

after put into session then it will store where you want to store like DB or file

you can also setup config->session.php

Teleogrid's avatar

I can set and get a session variable but there is no entry in my database beeing created.

I've set it to database within config->session.php as mentioned above. So it should be saved in database but it's not.

I have also set the following in session.php:

    'driver' => 'database',
    'connection' => 'mysql',
    'table' => 'sessions',
Teleogrid's avatar

I discovered what the problem was:

In my session table migration file I added a database column to the session table because I need a custom value to keep track of. But the driver seems to fail once there are custom fields in the session table.

What is the best practice to accomplish custom fields in the session table?

And where is my custom session data beeing saved? I can not find it in the database.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Does your new column have a default value or is it nullable? If not, make sure that it is :)

Teleogrid's avatar

Great, that seems to have been the problem. I only made it a unique() but not nullable. Thanks a lot!

Can you tell me a good way how to discover such a problem in the future, if there is something wrong with with database queries? I had a look into storage->logs but the database errors were not listed there. So It took me quite a while to figure out what was wrong.

Sinnbeck's avatar

Sadly I have not looked into the implementation of the database session driver, but a good rule of thumb is

if something out of my control add data to table, make sure that all changes will fail gracefully (meaning if you add a row, the session driver does not know of the new column and mysql will not add the row as you are missing data for a column)

Please or to participate in this conversation.