From 4fedd72ecbb1a8a0a3980ee700daa59a24dcbd11 Mon Sep 17 00:00:00 2001 From: kaisersly Date: Mon, 26 Sep 2022 15:01:24 +0200 Subject: [PATCH 1/3] Update tenants.blade.php Updates the Tenants topic to explain how to access the central context from a tenant's perspective. --- source/docs/v3/tenants.blade.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/source/docs/v3/tenants.blade.md b/source/docs/v3/tenants.blade.md index c85acdd..ca00985 100644 --- a/source/docs/v3/tenants.blade.md +++ b/source/docs/v3/tenants.blade.md @@ -130,6 +130,23 @@ You may access the current tenant using the `tenant()` helper. You can also pass Alternatively, you may typehint the `Stancl\Tenancy\Contracts\Tenant` interface to inject the model using the service container. +## Accessing the central application {#accessing-the-central-application} + +When your code executes in the context of a tenant (for instance, when the `DatabaseMigrated` event is dispatched), you may access the central application context by using the `tenancy()->central($callback)` function. + +For instance, if you want to retrieve the list of users of the central application when the code executes in a tenant's context, you may write: + +```php +public function getCentralUsers() +{ + // here we are in the tenant's context. \App\Models\User::all() would return the current tenant's users. + return tenancy()->central(function () { + // here we are in the central app context + return \App\Models\User::all(); + }); +} +``` + ## Incrementing IDs {#incrementing-ids} By default, the migration uses `string` for the `id` column, and the model generates UUIDs when you don't supply an `id` during tenant creation. From 07584b88f74761c5f1808a4bba85d4e885bfda28 Mon Sep 17 00:00:00 2001 From: kaisersly Date: Wed, 28 Sep 2022 08:38:41 +0200 Subject: [PATCH 2/3] Update tenants.blade.md See https://github.com/stancl/tenancy-docs/pull/207 --- source/docs/v3/tenants.blade.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/source/docs/v3/tenants.blade.md b/source/docs/v3/tenants.blade.md index ca00985..7dce6d6 100644 --- a/source/docs/v3/tenants.blade.md +++ b/source/docs/v3/tenants.blade.md @@ -132,19 +132,16 @@ Alternatively, you may typehint the `Stancl\Tenancy\Contracts\Tenant` interface ## Accessing the central application {#accessing-the-central-application} -When your code executes in the context of a tenant (for instance, when the `DatabaseMigrated` event is dispatched), you may access the central application context by using the `tenancy()->central($callback)` function. +When your code executes in the context of a tenant, you may access the central application context by using the `tenancy()->central($callback)` function. For instance, if you want to retrieve the list of users of the central application when the code executes in a tenant's context, you may write: ```php -public function getCentralUsers() -{ - // here we are in the tenant's context. \App\Models\User::all() would return the current tenant's users. - return tenancy()->central(function () { - // here we are in the central app context - return \App\Models\User::all(); - }); -} +// here we are in the tenant's context. \App\Models\User::all() would return the current tenant's users. +$centralUsers = tenancy()->central(function () { + // here we are in the central app context + return \App\Models\User::all(); +}); ``` ## Incrementing IDs {#incrementing-ids} From ddff134223b457010dfa144184443ad694827540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 28 Sep 2022 14:34:06 +0200 Subject: [PATCH 3/3] Update tenants.blade.md --- source/docs/v3/tenants.blade.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/docs/v3/tenants.blade.md b/source/docs/v3/tenants.blade.md index 7dce6d6..0e25e38 100644 --- a/source/docs/v3/tenants.blade.md +++ b/source/docs/v3/tenants.blade.md @@ -137,10 +137,10 @@ When your code executes in the context of a tenant, you may access the central a For instance, if you want to retrieve the list of users of the central application when the code executes in a tenant's context, you may write: ```php -// here we are in the tenant's context. \App\Models\User::all() would return the current tenant's users. +// Here we are in the tenant's context. User::all() would return the current tenant's users $centralUsers = tenancy()->central(function () { - // here we are in the central app context - return \App\Models\User::all(); + // Here we are in the central context + return User::all(); }); ```