First of all, you should define a secret connection in app/conifg/database.php and then in connections , for example:
'second_db_conn' => [ 'driver' => 'mysql', 'host' => $_SERVER['MYSQL_HOST'], 'database' => $_SERVER['MYSQL_DATABASE'], 'username' => $_SERVER['MYSQL_USERNAME'], 'password' => $_SERVER['MYSQL_PASSWORD'], 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ],
Then establish this connection on your model - save it - and return to the default (in my example it is mysql ):
$model->setConnection('second_db_conn')->save(); $model->setConnection('mysql');
source share