7

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 multiple databases in .env

where we need to read 2nd connection ...

Govind Samrow
  • 9,981
  • 13
  • 53
  • 90

2 Answers2

22

First, you'll need to configure your connections. If you don't already have one you'll need to create a config directory in your project and add the file config/database.php. It might look like this:

<?php

return [

   'default' => 'accounts',

   'connections' => [
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST'),
            'port'      => env('DB_PORT'),
            'database'  => env('DB_DATABASE'),
            'username'  => env('DB_USERNAME'),
            'password'  => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
         ],

        'mysql2' => [
            'driver'    => 'mysql',
            'host'      => env('DB2_HOST'),
            'port'      => env('DB_PORT'),
            'database'  => env('DB2_DATABASE'),
            'username'  => env('DB2_USERNAME'),
            'password'  => env('DB2_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
    ],
];

Once you've added your connection configurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name').

// Use default connection
app('db')->connection()->select('xx');
DB::connection()->select('yy');

// Use mysql2 connection
app('db')->connection('mysql2')->select('xx');
DB::connection('mysql2')->select('yy');

Hope this helps you!!

Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48
  • this file is under `/vendor/laravel/luman-framework/` directory. Is it ok to modify such a file? It seems it should be a way to put this in a place where the repository could notify the rest of the devs group that they need to define new env variables. Any ideas? – Gabo Apr 10 '18 at 18:40
  • @GaboLato: Hi, thanks for question! It's not good to make changes in `vendor` folder. Please find that file and if you want to see more info visit this link: https://stackoverflow.com/questions/37215265/lumen-create-database-connection-at-runtime – Hiren Gohel Apr 11 '18 at 06:59
  • Because if you make changes in `vendor` folder, it'll only change in your local server not in your prod server and you need to make changes also in your prod server's `vendor` folder which is not a good practice! – Hiren Gohel Apr 11 '18 at 07:04
  • 3
    I found out that if you create a directory `/config` and a file `database.php` in your project's root directory lumen will overwrite the vendor's file. Thanks! – Gabo Apr 11 '18 at 15:50
  • 1
    one other thing that needs to be added in bootstrap/app.php is $app->configure('database'); – Phani Shashank Aug 06 '20 at 17:26
5

This also worked. In the current version of Lumen 5.7

config/database.php

<?php

return [

    'default' => 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

Paul
  • 73
  • 2
  • 8