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
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.
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.
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)