1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 04:34:02 +00:00

Add tenancy()->central() helper

This commit is contained in:
Samuel Štancl 2020-11-12 17:07:57 +01:00
parent 1c93360077
commit 51ba97dbcc
3 changed files with 79 additions and 0 deletions

View file

@ -8,6 +8,13 @@ use Stancl\Tenancy\Contracts\Tenant;
trait TenantRun
{
/**
* Run a callback in this tenant's context.
* Atomic, safely reverts to previous context.
*
* @param callable $callback
* @return void
*/
public function run(callable $callback)
{
/** @var Tenant $this */

View file

@ -103,6 +103,29 @@ class Tenancy
return $this->model()->where($this->model()->getTenantKeyName(), $id)->first();
}
/**
* Run a callback in the central context.
*
* @param callable $callback
* @return mixed
*/
public function central(callable $callback)
{
$previousTenant = $this->tenant;
$this->end();
// This callback will usually not accept arguments, but the previous
// Tenant is the only value that can be useful here, so we pass that.
$result = $callback($previousTenant);
if ($previousTenant) {
$this->initialize($previousTenant);
}
return $result;
}
/**
* Run a callback for multiple tenants.
* More performant than running $tenant->run() one by one.