components->info('Removing pending tenants.'); $expirationDate = now(); // We compare the original expiration date to the new one to check if the new one is different later $originalExpirationDate = $expirationDate->copy()->toImmutable(); $olderThanDays = (int) $this->option('older-than-days'); $olderThanHours = (int) $this->option('older-than-hours'); if ($olderThanDays && $olderThanHours) { $this->components->error("Cannot use '--older-than-days' and '--older-than-hours' together. Please, choose only one of these options."); return 1; // Exit code for failure } if ($olderThanDays) { $expirationDate->subDays($olderThanDays); } if ($olderThanHours) { $expirationDate->subHours($olderThanHours); } $deletedTenantCount = tenancy()->query() ->onlyPending() ->when($originalExpirationDate->notEqualTo($expirationDate), function (Builder $query) use ($expirationDate) { $query->where($query->getModel()->getColumnForQuery('pending_since'), '<', $expirationDate->timestamp); }) ->get() ->each // Trigger the model events by deleting the tenants one by one ->delete() ->count(); $this->components->info($deletedTenantCount . ' pending ' . str('tenant')->plural($deletedTenantCount) . ' deleted.'); return 0; } }