I ran into the same problem and Joe's answer didn’t work in my case, since I have different database connections (so there are different hosts, ports, users and passages).
Therefore, migration must constantly perform many reconnections:
- Migration starts with the default database (in my case it is client_1)
- Selects material from the
migrations and clients table - Disable the default database
- Connect to client_2 database, start migration parts, disable client_2
- Connect to the default database again, save the "log" migration
And then loop it for each client.
public function up() { $defaultConnection = BackendConfig::getDatabaseConfigArray(); $clients = ClientController::returnDatabasesForArtisan(); foreach ($clients as $client) { BackendConfig::setDatabaseFromClient($client); Schema::create('newtable', function (Blueprint $table) { $table->increments('id')->unsigned(); $table->timestamps(); }); BackendConfig::setDatabaseFromArray($defaultConnection); } }
And the class in which the magic is stored:
class BackendConfig { public static function getDatabaseConfigArray($client_id = 1) { $connection = config('database.default'); return [ 'id' => $client_id, 'host' => config("database.connections.$connection.host"), 'port' => config("database.connections.$connection.port"), 'username' => config("database.connections.$connection.username"), 'password' => config("database.connections.$connection.password"), ]; } public static function setDatabaseFromArray($array) { self::setDatabase($array['id'], $array['host'], $array['port'], $array['username'], $array['password'], true); DB::disconnect(); } public static function setDatabaseFromClient(Client $client) { DB::disconnect(); self::setDatabase($client->id, $client->database->host, $client->database->port, $client->database->username, $client->database->password, true); } public static function setDatabase($client_id, $host, $port, $username, $password) { $connection = config('database.default'); $database_name = $connection . '_' . $client_id; config([ "database.connections.$connection.database" => $database_name, "database.connections.$connection.host" => $host, "database.connections.$connection.port" => $port, "database.connections.$connection.username" => $username, "database.connections.$connection.password" => $password, ]); }
With this solution, I can perform the same migrations on each client, but the migration is just saved in client_1, my type is the main client.
However, pay attention to two DB::disconnect(); . This will aggravate the situation because migration logs are stored in another client database or one.
And by the way, ClientController does nothing special:
public static function returnDatabasesForArtisan() { return Client::select('*')->with('database')->get(); }