1
votes

I encounter issues with the database driver in order to store sessions cookies:

The database connection is currently working but cookies are not set in the browser however session data are in my database.

I have run:

php artisan session:table

Set database session in my .env file:

SESSION_DRIVER=database

Bellow the config/session.php file:

[
    'driver' => env('SESSION_DRIVER', 'file'),
    'connection' => 'mysql',
    'table' => 'sessions',
]

EDIT 1:

The route in web.php:

Route::group(['domain' => GetDomainInfo::GetDomainName(), 'middleware' => 'web'], function () {
    Route::get('/', "HomeController@index");
});

Solved issue by :

I have created for example a localhost domain: webcv by adding to my hosts file:

127.0.0.1 webcv.local
127.0.0.1 admin.webcv.local

Set the session domain to : .webcv.local in config/session.php like so:

'domain' => env('SESSION_DOMAIN', ".webcv.local"),

Or just by adding in the .env file:

SESSION_DOMAIN=.webcv.local

Now I can access to both webcv.local and admin.webcv.local with the same cookie.

1
Please, provide more info about the problem. What's the error? - Frondor
there is no error but when i check the cookies on the browser there is nothing is stored - loic.lopez
php artisan session:table composer dump-autoload php artisan migrate - RïshïKêsh Kümar
@RïshïKêshKümar i have already run these commands - loic.lopez

1 Answers

3
votes

When you work with the database driver for sessions, you have to create the session table.

I'll assume you already scaffold the native authentication system by doing php artisan make:auth and migrated the migrations.

So first you generate the migration, and then migrate: php artisan session:table

Execute the migration: php artisan migrate

Obviously, this won't work if the routes you're checking on, aren't using the web middleware. If you're not registering the routes in /routes/web.php, then you have to manually specify the web middleware for them. Either in your controller's constructor, or directly in the route itself.

Edit after fix for future reference:

Also, check your domain in config/session.php is the same you're using in development or production, depending where you are working on.