我有一个自定义控制台命令,需要遍历一些公司并为每个公司创建一个数据库。该命令现在有效,但只有一次。它不会在循环中第二次运行迁移。所以基本上如果我有 2 个空白数据库(db_on
e 和db_two
),配置运行并迁移第一次db_one
,当循环再次运行时,没有任何反应db_two
。如果我替换控制台命令以仅回显数据库名称,它会返回两者,所以我知道它不是循环。此外,如果我注销循环的每个连接中使用的数据库,它就是正确的数据库。
这是命令:
public function handle()
{
$companies = Company::all();
foreach( $companies as $company)
{
\Config::set('database.connections.company.database', $company->db_name);
Artisan::call( 'migrate', [
'--database' => 'company',
'--path' => 'database/migrations/company',
]);
$this->info( config('database.connections.company.database') );
}
}
日志错误:
local.ERROR: Database [10_barrel] not configured. {"userId":1,"email":"packy@sites.com","exception":"[object] (InvalidArgumentException(code: 0): Database [db_2] not configured. at /Users/Sites/cheers/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:140)
数据库.php
'connections' => [
'main' => [
'driver' => 'mysql',
'host' => env('MAIN_DB_HOST', '127.0.0.1'),
'port' => env('MAIN_DB_PORT', '3306'),
'database' => env('MAIN_DB_DATABASE', 'forge'),
'username' => env('MAIN_DB_USERNAME', 'forge'),
'password' => env('MAIN_DB_PASSWORD', ''),
'unix_socket' => env('MAIN_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'company' => [
'driver' => 'mysql',
'host' => env('COMPANY_DB_HOST', '127.0.0.1'),
'port' => env('COMPANY_DB_PORT', '3306'),
'database' => '',
'username' => env('COMPANY_DB_USERNAME'),
'password' => env('COMPANY_DB_PASSWORD'),
'unix_socket' => env('COMPANY_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
]
环境:
COMPANY_DB_HOST=127.0.0.1
COMPANY_DB_PORT=3306
COMPANY_DB_USERNAME=root
COMPANY_DB_PASSWORD=