When I am seeding multiple databases I am running into a problem. The first database seed works fine. Seeding the second database however fails.
For the first Database I create the connection with a name of 'tenant' with the appropriate settings. like this...
$connections = Config::get('database.connections');
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $this->database,
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections['tenant'] = $tenant_connection;
Config::set('database.connections', $connections);
var_dump(Config::get('database.connections')); //its in there!!
For the second database I keep the connection name the same, and I change the database name in the connection... real simple.
Laravel is saving this connection. However, it still uses the original database. Say I create a connection named 'tenant' with database named database_1, seed works fine. Then I modify 'tenant' a connection with database_2. The seed runs on database_1.
I read that Laravel will not re-configure an existing database connection. Therefore Adding
DB::reconnect('tenant');
Seems to get me to using the database, however the migration is now failing, complaining that the migrations table does not exist, which for some reason it does not.
Here is the command to migrate, which is tested and (usually) works correctly:
Artisan::call('migrate', [
'--path' => "database/migrations_tenant",
'--database' => $this->dbc, //which is 'tenant'
]);
That is where I am stuck in modifying a database using the same connection name.
If I create a new connection name for each database I can run the migrations successfully. Unfortunately there are many complications finding the database name when using the eloquent model when using a variable database connection name. I do have this working modifying the default connection and leaving the eloquent connection variable uninitialized, not a great solution. Understanding how to keep the connection name the same would be preferred.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire