Merge branch 'master' of github.com:stancl/tenancy-docs

This commit is contained in:
Samuel Štancl 2020-01-27 18:24:53 +01:00
commit 8696ea497d
3 changed files with 37 additions and 2 deletions

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']]);
});
```

View file

@ -15,3 +15,22 @@ section: content
### For the central app: {#activitylog-central} ### For the central app: {#activitylog-central}
- Set the `database_connection` key in `config/activitylog.php` to the name of your central database connection. - 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.

View file

@ -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. 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: 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:
``` ```