From 0ee54da29c619f2617e4a47ec40626c05115e083 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Thu, 29 Sep 2022 10:03:45 +0200 Subject: [PATCH] Correct the tenant order in Run command --- src/Commands/Run.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Commands/Run.php b/src/Commands/Run.php index ebdb8767..94dc461c 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Commands; use Illuminate\Console\Command; use Illuminate\Contracts\Console\Kernel; +use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Concerns\HasTenantOptions; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\ConsoleOutput; @@ -34,7 +35,17 @@ class Run extends Command public function handle() { $argvInput = $this->ArgvInput(); - tenancy()->runForMultiple($this->getTenants(), function ($tenant) use ($argvInput) { + $tenants = $this->getTenants(); + + if ($this->option('tenants')) { + // $this->getTenants() doesn't return tenants in the same order as the tenants passed in the tenants option + // Map the passed tenant keys to the fetched tenant models to correct the order + $tenants = array_map(function (string $tenantKey) use ($tenants) { + return $tenants->filter(fn(Tenant $tenant) => $tenant->getTenantKey() === $tenantKey)->first(); + }, $this->option('tenants')); + } + + tenancy()->runForMultiple($tenants, function ($tenant) use ($argvInput) { $this->line("Tenant: {$tenant->getTenantKey()}"); $this->getLaravel()