I've a mysql connection and a pgsql (postgres, set as default) connection.
I must read all rows from a table from mysql and then save all into postgres
I tried do this, remembering that my default connection is pgsql
$mysql_model = new Regioni();
$regioni = $mysql_model->setConnection("mysql")->all();
But it's still using pgsql connection.
I'm sure of this because i tried to insert a row into pgsql table, (it was empty), and I can dump this single row as results of all.
Also, I (only for debugging, do not hate me), modified vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php, adding a debug echo into this method
/**
* Set the connection associated with the model.
*
* @param string|null $name
* @return $this
*/
public function setConnection($name)
{
echo "Model calling setConnection($name)" . PHP_EOL;
$this->connection = $name;
return $this;
}
As a result I can see
Model calling setConnection(mysql)
Model calling setConnection()
Model calling setConnection(pgsql)
The first one is the result of my explicit setConnection.
But why calling ->all() will reset connection to default one?
Main goal/question
What's the right way to dinamically change the connection of a model?