database = $database; $this->setName('tenants:migrate'); $this->specifyParameters(); } /** * Execute the console command. * * @return mixed */ public function handle() { if (! $this->confirmToProceed()) { return; } tenant()->all($this->option('tenants'))->each(function ($tenant) { $this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})"); // See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection. // Database connections are cached by Illuminate\Database\ConnectionResolver. $connectionName = "tenant{$tenant['uuid']}"; // todo use Illuminate DatabaseManager reconnect()? $this->input->setOption('database', $connectionName); $this->database->connectToTenant($tenant, $connectionName); // Migrate parent::handle(); }); if (tenancy()->initialized) { tenancy()->switchDatabaseConnection(); } else { $this->database->disconnect(); } } }