diff --git a/assets/config.php b/assets/config.php index d5989b33..27c8aad6 100644 --- a/assets/config.php +++ b/assets/config.php @@ -82,6 +82,7 @@ return [ // not needed for tenancy to be bootstrapped. They are run // regardless of whether tenancy has been initialized. + // Stancl\Tenancy\Features\Timestamps::class, // Stancl\Tenancy\Features\TenantConfig::class, // Stancl\Tenancy\Features\TelescopeTags::class, // Stancl\Tenancy\Features\TenantRedirect::class, diff --git a/src/Features/Timestamps.php b/src/Features/Timestamps.php new file mode 100644 index 00000000..595b5257 --- /dev/null +++ b/src/Features/Timestamps.php @@ -0,0 +1,27 @@ +hook('tenant.creating', function ($tm, Tenant $tenant) { + $tenant->with('created_at', Date::now()); + $tenant->with('updated_at', Date::now()); + }); + + $tenantManager->hook('tenant.updating', function ($tm, Tenant $tenant) { + $tenant->with('updated_at', Date::now()); + }); + + $tenantManager->hook('tenant.softDeleting', function ($tm, Tenant $tenant) { + $tenant->with('deleted_at', Date::now()); + }); + } +} \ No newline at end of file diff --git a/src/Tenant.php b/src/Tenant.php index 124e3412..237697c7 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -8,6 +8,7 @@ use ArrayAccess; use Closure; use Illuminate\Config\Repository; use Illuminate\Foundation\Application; +use Illuminate\Support\Facades\Date; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; use Stancl\Tenancy\Contracts\Future\CanDeleteKeys; @@ -260,10 +261,16 @@ class Tenant implements ArrayAccess */ public function softDelete(): self { - $this->put('_tenancy_original_domains', $this->domains); + $this->manager->event('tenant.softDeleting', $this); + + $this->put([ + '_tenancy_original_domains' => $this->domains, + ]); $this->clearDomains(); $this->save(); + $this->manager->event('tenant.softDeleted', $this); + return $this; } @@ -391,7 +398,7 @@ class Tenant implements ArrayAccess } /** - * Set a value. + * Set a value in the data array without saving into storage. * * @param string $key * @param mixed $value