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()