我尝试设置多个数据库来运行迁移。 在“config / database.php”文件中,这是代码:
'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 :(得分:1)
在配置中使用数组键而不是数据库名称。那是
Schema::connection('mysql2')->create('test', function(Blueprint $table) {
$table->increments('id');
});