From fd1ca2fb10d75b1e0f00ce6b371209ba36109545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 17 Oct 2019 21:06:37 +0200 Subject: [PATCH] [2.1.0] Tenant run (#31) * tenant run * wip --- docs/source/v2/tenant-routes.blade.md | 2 +- docs/source/v2/tenants.blade.md | 29 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/source/v2/tenant-routes.blade.md b/docs/source/v2/tenant-routes.blade.md index 6c0b8fd..aef08f3 100644 --- a/docs/source/v2/tenant-routes.blade.md +++ b/docs/source/v2/tenant-routes.blade.md @@ -1,6 +1,6 @@ --- title: Tenant Routes -description: Tenant routes.. +description: Tenant Routes extends: _layouts.documentation section: content --- diff --git a/docs/source/v2/tenants.blade.md b/docs/source/v2/tenants.blade.md index 65334b7..56ec759 100644 --- a/docs/source/v2/tenants.blade.md +++ b/docs/source/v2/tenants.blade.md @@ -36,6 +36,35 @@ $tenant->removeDomains('foo.yourapp.com')->save(); > Don't forget to `->save()` after modifying the domains! +## `run()` {#run} + +The `$tenant->run()` command lets you execute a closure inside a tenant's "environment". +```php +$tenant->run(function ($tenant) { + User::create(['name' => 'Admin', 'email' => 'admin@yourapp.com', ...]); +}); +``` + +It also lets you get data from the tenant's environment: +```php +$tenantsUserCount = $tenant->run(function ($tenant) { + return User::count(); +}); +``` + +If you need access to the tenant within the closure, it's passed as the first argument. + +This feature is a safe alternative to: +```php +tenancy()->initialize($tenant); + +// make some changes + +tenancy()->end(); +``` + +and it also checks if tenancy was initialized. If it was, it returns to the original tenant after running the closure. + ## `$persisted` {#persisted} This property says whether the model has saved to the storage yet. In other words, if it's `false`, it's a new instance that has not been `->save()`d yet.