From 6bcc5cdd88c851f05bbf41ac247ba986cf7a3d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 27 Jan 2020 17:04:29 +0100 Subject: [PATCH] Dynamic connections --- docs/source/v2/custom-db-connections.blade.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/source/v2/custom-db-connections.blade.md b/docs/source/v2/custom-db-connections.blade.md index 1033263..dade6d9 100644 --- a/docs/source/v2/custom-db-connections.blade.md +++ b/docs/source/v2/custom-db-connections.blade.md @@ -8,3 +8,19 @@ section: content # Custom Database Connections {#custom-database-names} To set a specific database connection for a tenant, set the `_tenancy_db_connection` key in the tenant's storage. The connection's database name will be still replaced by the tenant's database name. You can [customize that]({{ $page->link('custom-database-names') }}) too. + +You may want custom connections to be dynamic (rather than adding them to the DB config manually), so can use something like this: + +```php +// Make new tenants use your connection "template" +Tenant::new()->withData([ + '_tenancy_db_connection' => 'someTenantConnectionTemplate', +]); + +// Make tweaks to the connection before bootstrapping tenancy +tenancy()->hook('bootstrapping', function ($tenantManager) { + config(['database.connections.someTenantConnectionTemplate.name' => $tenantManager->tenant['database_name']]); + config(['database.connections.someTenantConnectionTemplate.password' => $tenantManager->tenant['database_password']]); + config(['database.connections.someTenantConnectionTemplate.host' => $tenantManager->tenant['database_host']]); +}); +```