We\'ve using Lumen for building API\'s , Now we need to access multiple databases.
Currently using .env
for database config but unable to found the way to m
This also worked. In the current version of Lumen 5.7
config/database.php
env('DB_CONNECTION', 'sqlsrv'),
'migrations' => 'migrations',
'connections' => [
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
'sqlsrv2' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
];
.env
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=database1
DB_USERNAME=username
DB_PASSWORD=password
DB_DATABASE2=database2
Usage:
Model: protected $connection = 'sqlsrv2';
Other: ->connection('sqlsrv2')
I hope i help you!
Reference:https://fideloper.com/laravel-multiple-database-connections