Dynamic connections

This commit is contained in:
Samuel Štancl 2020-01-27 17:04:29 +01:00 committed by GitHub
parent 126bcd04ff
commit 6bcc5cdd88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,3 +8,19 @@ section: content
# Custom Database Connections {#custom-database-names} # 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. 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']]);
});
```