components->info('Creating pending tenants.'); $maxPendingTenantCount = (int) ($this->option('count') ?? config('tenancy.pending.count')); $pendingTenantCount = $this->getPendingTenantCount(); $createdCount = 0; while ($pendingTenantCount < $maxPendingTenantCount) { tenancy()->model()::createPending(); // Fetching the pending tenant count in each iteration prevents creating too many tenants // If pending tenants are being created somewhere else while running this command $pendingTenantCount = $this->getPendingTenantCount(); $createdCount++; } $this->components->info($createdCount . ' pending ' . str('tenant')->plural($createdCount) . ' created.'); $this->components->info($maxPendingTenantCount . ' pending ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.'); return 0; } /** Calculate the number of currently available pending tenants. */ protected function getPendingTenantCount(): int { return tenancy()->query() ->onlyPending() ->count(); } }