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']]); +}); +``` diff --git a/docs/source/v2/spatie.blade.md b/docs/source/v2/spatie.blade.md index ad595c6..04158b1 100644 --- a/docs/source/v2/spatie.blade.md +++ b/docs/source/v2/spatie.blade.md @@ -15,3 +15,22 @@ section: content ### For the central app: {#activitylog-central} - Set the `database_connection` key in `config/activitylog.php` to the name of your central database connection. + +## laravel-permission {#permission} + +Install the package like usual, but publish the migrations and move them to `migrations/tenant`: + +``` +php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations" +mv database/migrations/*_create_permission_tables.php database/migrations/tenant +``` + +Then add this to your `AppServiceProvider::boot()` method: + +```php +tenancy()->hook('bootstrapped', function (TenantManager $tenantManager) { + \Spatie\Permission\PermissionRegistrar::$cacheKey = 'spatie.permission.cache.tenant.' . $tenantManager->getTenant('id'); +}); +``` + +The reason for this is that spatie/laravel-permission caches permissions & roles to save DB queries, which means that we need to separate the permission cache by tenant. diff --git a/docs/source/v2/storage-drivers.blade.md b/docs/source/v2/storage-drivers.blade.md index 956df37..8a2e76b 100644 --- a/docs/source/v2/storage-drivers.blade.md +++ b/docs/source/v2/storage-drivers.blade.md @@ -17,9 +17,9 @@ The database storage driver lets you store tenant information in a relational da The benefit of this storage driver is that you don't have to use both Redis and a database for your data. Also you don't have to do as much configuration. -To use this driver, you need to have a `tenants` table and a `domains` table. You may also use a custom database connection. By default, `tenancy.storage.db.connection` is set to `null`, which means that your app's default database connection will be used to store tenants. +To use this driver, you need to have a `tenants` table and a `domains` table. You may also use a custom database connection. By default, `tenancy.storage_drivers.db.connection` is set to `null`, which means that your app's default database connection will be used to store tenants. -If you wish to use a different connection, e.g. `central`, you may create it in the `config/database.php` file and set `tenancy.storage.db.connection` to the name of that connection. It's recommended to do this for easier changes in the future. +If you wish to use a different connection, e.g. `central`, you may create it in the `config/database.php` file and set `tenancy.storage_drivers.db.connection` to the name of that connection. It's recommended to do this for easier changes in the future. To create the `tenants` and `domains` tables, you can use the migrations that come with this package. If you haven't published them during installation, publish them now: ```