Multiple databases in Lumen 5.2

It took me a couple of hours to figure out how to use multiple databases in Lumen 5.2, as much of the information about multiple databases was from when Lumen still had a config file.

You need to create a config directory under the root, call it database.php for sanity sake.

return [
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST'),
        'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],
        'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB2_HOST'),
            'database' => env('DB2_DATABASE'),
            'username' => env('DB2_USERNAME'),
            'password' => env('DB2_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],
    ]
];

Or however you want to name your databases and such. Then you need to add a line into bootstrap/app.php: $app->configure('database');

Now you can use multiple databases! E.g. app('db')->connection('mysql')->select('xx') or DB::connection('mysql2')->select('yy')