I'm trying to make a laravel use using multiple databases:
- Laravel version: 5.5.28
- Php Version 7.2.0
- Database Driver & version: MariaDB 10.1.29
Define Connections:
return array(
'default' => 'mysql',
'connections' => array(
# Primary/Default database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'bdd1',
'username' => 'root',
'password' => ''
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'bdd2',
'username' => 'root',
'password' => ''
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
Define model:
class Products extends Model {
protected $connection = 'mysql';
}
Define controller:
class ProductController extends BaseController {
public function find()
{
$productModel= new Products;
$productModel->setConnection('mysql2');
$product= $productModel->find(1);
return $product;
}
}
The code above works without error, but if I change the name of the connection in the controller it continues using what was configured in the .env file
Please, could anyone help me solve this problem?