Laravel: Run migrations on another database

前端 未结 8 2064
有刺的猬
有刺的猬 2020-12-29 23:15

In my app every user has its own database that created when user registered. Connection and database data (database name, username, password) are saved in a table in default

8条回答
  •  时光取名叫无心
    2020-12-29 23:52

    In your app/config/database.php you have to:

     'mysql',
    
        'connections' => array(
    
            # Our primary database connection
            'mysql' => array(
                'driver'    => 'mysql',
                'host'      => 'host1',
                'database'  => 'database1',
                'username'  => 'user1',
                'password'  => 'pass1'
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),
    
            # Our secondary database connection
            'mysql2' => array(
                'driver'    => 'mysql',
                'host'      => 'host2',
                'database'  => 'database2',
                'username'  => 'user2',
                'password'  => 'pass2'
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),
        ),
    );
    

    Now that you prepared two database connections in your migration you can do:

    Schema::connection('mysql2')->create('some_table', function($table)
    {
        $table->increments('id');
    });
    

    This should work. More infos on: http://fideloper.com/laravel-multiple-database-connections

提交回复
热议问题