简体   繁体   中英

Multiple database config file with Laravel

I've developed a Web application with Laravel and everything is ok.

This should be a multi-tenant application, so I would like to share the same code but to use a different database for each tenant (I decided for this architecture as according to me it is too complex to share database schema or records among tenants). Every tenant is accessing the application with its own third level domain (tenant1.xxxx.com, tenant2.xxxx.com, etc)

I would like then to create n. databases (tenant1, tenant2, etc) and to create n. database config file in Laravel (database.tenant1.php, database.tenant2.php, etc)

The problem now is that I cannot find an elegant way to alter the database config file loading system in Laravel. I should select the config file, based on the host name used by the customer.

Any help would be appreciated.

Thank you, Michele

in your config folder, there is a 'database.php' file. in that file you can see an array called 'connection' .
'connection' array manages different configuration for multiple database connections.
you can define your configuration for each of tenants in 'connection' array and based on the scenario you are in it, you can choose the appropriate connection to handle your query with a syntax like this:

DB::connection('tenant1')->select('where...');

or
DB::connection('tenant2')->select('where...');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM