1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:14:04 +00:00

Pending tenants: Add getPendingAttributes()

This method lets the user specify default values for custom
non-nullable columns. The primary use case is when the tenants table
has a column like 'slug' and createPending() is called with no
value for 'slug'. This would produce an exception due to the column
having no default value.

Here, getPendingAttributes() can set an initial dummy slug (like a
randomly generated string) before it's overwritten during a pull.

getPendingAttributes() accepts an $attributes array which corresponds
to the attributes passed to createPending(). The array returned from
getPendingAttributes() is ultimately merged with $attributes, so
the user doesn't need to use the $attributes value in
getPendingAttributes(), however it serves to provide more context when
the pending attributes might be dependent on $attributes and therefore
derived from the $attributes actually being used.

Also fixed the `finally` branch in createPending() as it was
potentially referencing the $tenant variable before it was initialized.
This commit is contained in:
Samuel Štancl 2025-10-27 17:54:39 +01:00
parent aba7a50619
commit 6523f24a60
3 changed files with 55 additions and 2 deletions

View file

@ -49,13 +49,15 @@ trait HasPending
*/
public static function createPending(array $attributes = []): Model&Tenant
{
$tenant = null;
try {
$tenant = static::create($attributes);
$tenant = static::create(array_merge(static::getPendingAttributes($attributes), $attributes));
event(new CreatingPendingTenant($tenant));
} finally {
// Update the pending_since value only after the tenant is created so it's
// not marked as pending until after migrations, seeders, etc are run.
$tenant->update([
$tenant?->update([
'pending_since' => now()->timestamp,
]);
}
@ -65,6 +67,17 @@ trait HasPending
return $tenant;
}
/**
* Attributes to be set when a pending tenant is initially created.
*
* @param array<string, mixed> $attributes The attributes passed to createPending() (will be merged with the returned array)
* @return array<string, mixed>
*/
public static function getPendingAttributes(array $attributes): array
{
return [];
}
/**
* Pull a pending tenant from the pool or create a new one if the pool is empty.
*