Laravel Artisan command on database connection

6.3k views Asked by At

I'm working on a projects where I've multiple database connections. I'm able to run artisan command from my controller like Artisan::call('migrate', array('--path' => 'app/database/migration'));

However this runs on my default database connection pretty well. Now I'm looking for a way to call artisan command for other dynamic database connection. I know I can specify database name in my command like Artisan::call('migrate', array('--database' => 'myDatabase', '--path' => 'app/database/migration/myCustomMigration')); but it's not working as expected. It's still running command on my default database connection.

Is there any way to do so as I can run work with eloquent like...

$user = new User;
$user->setConnection('myDatabaseConnectionKey');
$user->email = $email;
$user->password = Hash::make('password');
$user->first_name = 'First name';
$user->last_name = 'Last name';
$user->created_at = new DateTime();
$user->updated_at = new DateTime();
$user->save();

Thanks in advance.

1

There are 1 answers

3
Alexander Kerchum On

You should consider setting up a different environment for the different database connection. Then you could invoke any artisan command in that environent by using the --env flag.

Laravel Docs for Enviroment Configuration