KoalaGangsta
KoalaGangsta

Reputation: 556

Call Config::set to change database in Lumen 5.4? (Using Eloquent)

I have a small application in Lumen which should handle multiple databases based on the request URL. To make this possible in the first place I've created a folder /config/ and a config-file called /config/database.php. Here is the code and I added some comments to show what I exactly need to do.

<?php
return [
    'migrations' => 'home',
    'default' => 'home',
    'connections' => [
        // This is the default option and its needed to check if the "client" exists.
        'home' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'home',
            'username'  => 'root',
            'password'  => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],    

        // When the client is found in the "home" connection, I'd like to continue with this database settings as default.
        'client' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'client_', // There is one database per client which in the end will look like this: "client_******", so I have to add this using Config:set...
            'username'  => 'root',
            'password'  => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
    ],
];

I have to call Config::set to change these settings but it doesnt seem to be available on Lumen. How can i change these settings (the default database connection and its database)

I hope there is a way to make this possible. Thanks a lot :)

Upvotes: 1

Views: 2359

Answers (2)

rikardo_paiva
rikardo_paiva

Reputation: 393

In a query you can use as follows:

$connectionClient = DB::connections('client'); $connectionHome = DB::connections('home');

I hope have helped.

Upvotes: 0

KoalaGangsta
KoalaGangsta

Reputation: 556

I found the solution.

To set and edit your configuration use the "config" helper method with an array as argument.

Example:

config(['database.default' => 'your_connection']);

Upvotes: 1

Related Questions