mirror of
https://github.com/stancl/tenancy-docs.git
synced 2025-12-12 02:04:03 +00:00
Data column explanation + updating tenants
I've clarified how the data column works along with how to update a tenant's custom column and an attribute in its data column. I've also cleared up how the getCustomColumns() function works (When I first used the package I thought it meant to define columns that will be used in the data column).
This commit is contained in:
parent
97b275ffba
commit
922586e539
1 changed files with 19 additions and 8 deletions
|
|
@ -16,9 +16,8 @@ The base model has the following features on top of the ones that are necessary
|
|||
- Data column trait — lets you store arbitrary keys. Attributes that don't exist as columns on your `tenants` table go to the `data` column as serialized JSON.
|
||||
- Id generation trait — when you don't supply an ID, a random uuid will be generated. An alternative to this would be using AUTOINCREMENT columns. If you wish to use numerical ids, change the `create_tenants_table` migration to use `bigIncrements()` or some such column type, and set `tenancy.id_generator` config to null. That will disable the ID generation altogether, falling back to the database's autoincrement mechanism.
|
||||
|
||||
**Most** applications using this package will want domain/subdomain identification and tenant databases.
|
||||
|
||||
To do this, create a new model, e.g. `App\Tenant`, that looks like this:
|
||||
## Tenant Model
|
||||
**Most** applications using this package will want domain/subdomain identification and tenant databases. To do this, create a new model, e.g. `App\Tenant`, that looks like this:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
|
@ -60,9 +59,22 @@ After the tenant is created, an event will be fired. This will result in things
|
|||
|
||||
## Custom columns {#custom-columns}
|
||||
|
||||
Attributes of the tenant model which don't have their own column will be stored in the `data` JSON column.
|
||||
Attributes of the tenant model which don't have their own column will be stored in the `data` JSON column. You can set a custom attribute as you normally would set a models atribute:
|
||||
|
||||
You may define the custom columns be overriding the `getCustomColumns()` method on your `Tenant` model:
|
||||
```php
|
||||
$tenant->update([
|
||||
'customAttribute' => 'value', // to be store in the `data` JSON column
|
||||
'plan' => 'free' // to be stored in the plan column (see below)
|
||||
]);
|
||||
```
|
||||
or simply
|
||||
```php
|
||||
$tenant->customAttribute = 'value'; // to be store in the `data` JSON column
|
||||
$tenant->plan = 'free'; // to be stored in the plan column (see below)
|
||||
$tenant->save();
|
||||
```
|
||||
|
||||
You may define the custom columns that **won't** be used in the `data` JSON column by overriding the `getCustomColumns()` method on your `Tenant` model:
|
||||
|
||||
```php
|
||||
public static function getCustomColumns(): array
|
||||
|
|
@ -70,7 +82,6 @@ public static function getCustomColumns(): array
|
|||
return [
|
||||
'id',
|
||||
'plan',
|
||||
'locale',
|
||||
];
|
||||
}
|
||||
```
|
||||
|
|
@ -97,7 +108,7 @@ Also a good rule of thumb is that when you need to query the data with `WHERE` c
|
|||
|
||||
## Running commands in the tenant context {#running-commands-in-the-tenant-context}
|
||||
|
||||
You may run commands in a tenant's context and then return to the previous context (be it central, or another tenant's) by passing a callable to the `run()` method on the tenant object. For example:
|
||||
You may run commands in a tenant's context (e.g. creating a user in the tenants user database) and then return to the previous context (be it central, or another tenant's) by passing a callable to the `run()` method on the tenant object. For example:
|
||||
|
||||
```php
|
||||
$tenant->run(function () {
|
||||
|
|
@ -130,4 +141,4 @@ public function getIncrementing()
|
|||
{
|
||||
return true;
|
||||
}
|
||||
```
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue