Change DB connection on the fly

Amazing solution:
(But this won’t work for eloquents)

Override getConnectionName() of model


Test function

Global Middleware


The position you put the middleware in is critical. Since cookie is used in the middleware, you can’t put it in global middleware because cookie and session are not activated yet.

The correct position is in “web” group


  2. Override DB facade
  3. Extending DB facade
  4. Change database configs
  5. Extend model (For eloquent)