Skip to content

DatabaseSessionBootstrapper

The DatabaseSessionBootstrapper makes the database session driver compatible with the package. It only does anything when session.driver is database.

When tenancy is initialized, it sets session.connection to tenant and makes the session driver use the tenant connection. When tenancy is ended, it switches the session driver back to the original connection (or the central connection if session.connection wasn’t set).

The bootstrapper is enabled by default:

config/tenancy.php
'bootstrappers' => [
// ...
// Adds support for the database session driver
Bootstrappers\DatabaseSessionBootstrapper::class,
],

With multi-database tenancy, the DatabaseTenancyBootstrapper alone already stores sessions in tenant databases, since the session driver uses the default connection. However, without this bootstrapper you may get exceptions like Call to a member function prepare() on null when switching between tenants.

If you set a SESSION_CONNECTION, the bootstrapper is necessary to separate tenant sessions. Without it, the session driver would keep using that connection in the tenant context, so tenant sessions wouldn’t be stored in the tenant database — allowing for session forgery.

With Postgres RLS, the bootstrapper is required as well, and it has to come after the PostgresRLSBootstrapper. See Session scoping for the full setup.