How can I run migration on a connection #48356
-
I have the following scenario where I create a connection using capsule and I want to perform one or more actions using this connection that was created at run time, for example:
The problem I'm having is that this code above is being executed on my project's default connection. Would anyone know a way to do something in this context? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey there. You can achieve this using the 'sqlite' => [...],
'mysql => [...],
+ 'dynamic' => [] Then, using the // The default connection
$default = config('database.connections.mysql');
// Merge the default connection values with the new connection values.
config([
'database.connections.dynamic' => array_merge($default, [
'username' => 'root',
'database' => 'forge',
#... other options
])
]);
// Now the framwork migrator will use the new database connection.
Artisan::call('migrate', [
'--database' => 'dynamic',
'--seed' => true
]); |
Beta Was this translation helpful? Give feedback.
Hey there.
You can achieve this using the
config
helper to create a new connection on the fly.But first, you need to add an empty database name, like so:
'sqlite' => [...], 'mysql => [...], + 'dynamic' => []
Then, using the
config
helper, you can assign the new connection to the empty one: