3

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 Answers1

1

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

  • I've set env for my local development already. Still getting `Database [dbName] not configured.` where I already set connections for that and I can see that using `Config::get('database.connections');` – Mohammad Shoriful Islam Ronju Dec 25 '14 at 06:37
  • 3
    Okay! I was doing it wrong! `Artisan::call('migrate', array('--database' => 'connectionKey', '--env' => 'local', '--path' => 'app/database/migration/myCustomMigration'));` now that's working fine. – Mohammad Shoriful Islam Ronju Dec 25 '14 at 06:55
  • Thanks Mohammad! That was exactly what I was looking for! I was missing the --database parameter too – Poncho Jul 02 '15 at 00:01