Lumen - Create database connection at runtime Lumen - Create database connection at runtime laravel laravel

Lumen - Create database connection at runtime


There is one main problem with the method you are going for:

You did not initialize any configuration object. Lumen by default has no traditional config object set, until you create a config directory in your root folder.

As written in the Lumen configuration docs:

All of the configuration options for the Lumen framework are stored in the .env file.

The approach you are going for requires the traditional config object as used in Laravel.

To get that object and your new retail_db database connection working:

  • Create a config folder in your project root
  • Copy the file vendor/laravel/lumen-framework/config/database.php to this config folder
  • Initialize the database configuration object in your bootstrap/app.php with $app->configure('database'); (put it at line 28)

Your folder structure looks like this now:

├── app├── bootstrap├── config   └── database.php├── database├── public├── resources├── storage├── tests└── vendor

Of course you can remove those connections you don't need from the connections array in app/config/database.php by commenting or removing them completely.

app/config/database.php

'connections' => [        /*'testing' => [            'driver' => 'sqlite',            'database' => ':memory:',        ],*/        'sqlite' => [            'driver'   => 'sqlite',            'database' => env('DB_DATABASE', base_path('database/database.sqlite')),            'prefix'   => env('DB_PREFIX', ''),        ],        'mysql' => [            'driver'    => 'mysql',            'host'      => env('DB_HOST', 'localhost'),            'port'      => env('DB_PORT', 3306),            'database'  => env('DB_DATABASE', 'forge'),            'username'  => env('DB_USERNAME', 'forge'),            'password'  => env('DB_PASSWORD', ''),            'charset'   => env('DB_CHARSET', 'utf8'),            'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),            'prefix'    => env('DB_PREFIX', ''),            'timezone'  => env('DB_TIMEZONE', '+00:00'),            'strict'    => env('DB_STRICT_MODE', false),        ],]

Your bootstrap/app.php with the changes:

/*|--------------------------------------------------------------------------| Create The Application|--------------------------------------------------------------------------|| Here we will load the environment and create the application instance| that serves as the central piece of this framework. We'll use this| application as an "IoC" container and router for this framework.|*/$app = new Laravel\Lumen\Application(    realpath(__DIR__.'/../'));//$app->withFacades();// $app->withEloquent();$app->configure('database');

Now you can use the code you already have in your routes.php.

To delete your retail_db connection, just set it to null:

$config->set('database.connections.retail_db', null);


To answer @jhon's questionLumen does not provides that config folder, you need to create at root the new directory called config.

Then inside it just copy what @codedge mentions about database.php file

Copy the file vendor/laravel/lumen-framework/config/database.php to this config folder

follow below commands in terminal to create the config directory and copy the database.php file

user$ cd projectPath/user$ mkdir configuser$ cp vendor/laravel/lumen-framework/config/database.php config/database.php

where this config folder is the config directory you just created.so it would looks like the image below.folder structure