Daily Archives: 03/04/2020

Change DB connection on the fly

Amazing solution:

Test function

Global Middleware

app/Http/Kernel.php

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

https://laracasts.com/discuss/channels/laravel/session-doesnt-work-in-middleware

Reference:

  1. https://stackoverflow.com/questions/31041893/laravel-change-database-connection-run-time
  2. Override DB facade
    https://stackoverflow.com/questions/40614875/laravel-5-extend-a-facade/40615078#40615078
  3. Extending DB facade
    https://stackoverflow.com/questions/52944843/extending-db-facade-laravel
  4. Change database configs
    https://laracasts.com/discuss/channels/eloquent/how-to-modify-database-connection-string-in-laravel-5
  5. Extend model (For eloquent)
    https://stackoverflow.com/questions/28985472/change-database-connection-in-laravel-model