In the Request lifecycle, no database connections exists when we start it. Laravel will make sure to connect the
main connection automatically, but the
tenant connection is triggered in the 3rd stage of the request (bootstrap, route, middleware, controller). Each request will reconnect based on session information (could be token as well). The usage of the
reconnect word in the trait is because I don’t want to give the impression that you can
connect to as much tenants as you want. If you call
reconnect, you’re gonna DROP the current tenant connection and establish (reconnect) a new one. It also have the benefit of consistency with
DB::reconnect, which is how you tell Laravel to clean a connection configuration and start from scratch.
My Company model is not a tenant table, it is a main table. After connecting to the main database, I can get a list of tenants that I have from the Company model and connect to it’s specific database.