Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shibbir's avatar

How to run laravel migrate command to different web server

I have multiple hosting server for example : central_db, hosting_1, hosting_2, hosting_3 etc..

This hosting_1, hosting_2, hosting_3 details are saved on central_db database in mysql_servers table.

Now, I need to run this

php artisan migrate

command to create a tables to multiple hosting servers.

Now, I need to run this command to create a feed_mappings table to all hosting server .

So, What I am running is this:

// Get all mysql servers 
$mysql_servers = DB::table('mysql_servers')->get();
if( $mysql_servers ) {
    foreach($mysql_servers as $server ) {
        if( ! Schema::connection($server->mysql_db_name)->hasTable('feed_mappings') ) {
            Schema::connection($server->mysql_db_name)->create('feed_mappings', function (Blueprint $table) {
                $table->bigIncrements('id_feed_mappings')->index();
                $table->bigInteger('id_feed');
                $table->string('id_project', 255);
                $table->string('import_field_slug', 155);
                $table->string('internal_field_slug', 155)->nullable();
                $table->tinyInteger('import');
                $table->tinyInteger('custom_field')->nullable();
                $table->dateTime('date_add');
                $table->dateTime('date_upd');
            });
        }
    }
}

But it's not creating any table to different hosting server. Is anything I am doing wrong? How can I solve it?

0 likes
5 replies
Muetze's avatar

There are several possibilities.

Shibbir's avatar

@Muetze I want to go with the second option.

So, then shall I use the loop inside the config/database.php file? Or ....

Muetze's avatar

You just have to know how dynamic it should be.

You can of course also develop a function that outputs the connections exactly in the required config array and use this in the config/database.php. With change you must only make sure that the config cache is renewed in the production.

There are many possibilities.

With the on the fly method you would never have to touch the config again. (Should only not be set globally, so that on j

Please or to participate in this conversation.