Search code examples
laravelauthenticationdatabase-connectionsubdomain

Laravel Auth with different connection for each subdomain?


I'm building an app where each subdomain has its own database. For example:
"example1.app.dev" uses "example1_dbo" database
"example2.app.dev" uses "example2_dbo" database

Each subdomain has its own users, meaning that for example:
user_ex1 can only login on example1.app.dev because he is set in example1_dbo
user_ex2 can only login on example2.app.dev because he is set in example2_dbo

How do I achieve this with Laravel Auth? Basicaly I have set subdomain routing:

Route::domain('{account}.myapp.dev')->group(function () {})

And i have set up database connections in config/database.php and env file. I have used this concept on Eloquent models with Model->setConnection($account) But this method is exhausting while app is growing...

I'm looking for Middleware solution where i can change default DB connection for request globally and for Auth as well while i was not able to get authentication to work.


Solution

  • Have you tried this package: https://github.com/stancl/tenancy

    It provide that out of the box. hope it is helpful.