2017-04-10 150 views
0

我嘗試設置多個數據庫以運行遷移。 在文件「配置/ database.php中」這樣的代碼:在多個數據庫中遷移laravel

'mysql' => [ 
     'driver' => 'mysql', 
     'host' => env('DB_HOST', '127.0.0.1'), 
     'port' => env('DB_PORT', '3306'), 
     'database' => env('DB_DATABASE', 'first_database'), 
     'username' => env('DB_USERNAME', 'root'), 
     'password' => env('DB_PASSWORD', ''), 
     'unix_socket' => env('DB_SOCKET', ''), 
     'charset' => 'utf8mb4', 
     'collation' => 'utf8mb4_unicode_ci', 
     'prefix' => '', 
     'strict' => true, 
     'engine' => null, 
    ], 

    'mysql2' => [ 
     'driver' => 'mysql', 
     'host' => env('DB_SEC_HOST', '127.0.0.1'), 
     'port' => env('DB_SEC_PORT', '3306'), 
     'database' => env('DB_SEC_DATABASE', 'second_database'), 
     'username' => env('DB_SEC_USERNAME', 'root'), 
     'password' => env('DB_SEC_PASSWORD', ''), 
     'unix_socket' => env('DB_SOCKET', ''), 
     'charset' => 'utf8mb4', 
     'collation' => 'utf8mb4_unicode_ci', 
     'prefix' => '', 
     'strict' => true, 
     'engine' => null, 
    ], 

在文件「ENV」這樣的代碼:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=first_database 
DB_USERNAME=root 
DB_PASSWORD= 

DB_SEC_CONNECTION=mysql2 
DB_SEC_HOST=127.0.0.1 
DB_SEC_PORT=3306 
DB_SEC_DATABASE=second_database 
DB_SEC_USERNAME=root 
DB_SEC_PASSWORD= 

當運行與此代碼遷移:

Schema::connection('second_database')->create('test', function(Blueprint $table) { 
     $table->increments('id'); 
    }); 

終端示出了該錯誤消息:

[InvalidArgumentException] 
Database [second_database] not configured. 

我在做什麼錯?

謝謝!

回答

0

在配置中使用數組鍵而不是數據庫名稱。那就是

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