2

I need to use several databases in laravel, and i'm trying to create a form to add a new db connection to connections array in config/database.php.

I don't want to add connections manually, but throught a form.

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        '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' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],

],

Thanks

1
  • This link may be of help here Commented Nov 9, 2016 at 4:44

2 Answers 2

2

You simple add as many databases to the array as you require then add the connection values to you .env file or to the array valyes like so:

return array(

'default' => 'my_first_db',

'connections' => array(

    # Our primary database connection
    'my_first_db' => array(
        'driver'    => 'mysql',
        'host'      => 'host1',
        'database'  => 'database1',
        'username'  => 'user1',
        'password'  => 'pass1'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    # Our secondary database connection
    'my_second_db' => array(
        'driver'    => 'mysql',
        'host'      => 'host2',
        'database'  => 'database2',
        'username'  => 'user2',
        'password'  => 'pass2'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
),

);

Schema::connection('my_second_db')->create('a_new_table', function($table)
{
    $table->increments('id'):
});

$users = DB::connection('my_second_db')->select('users');

To add connections through a form you would need to post the data to a template similar to the above and then write to the server, This is a bit of a security risk doing that and would need to be carefull in what you do and how you go about doing it, Posting database data in a request and then just saving the database data to a file is a bit overboard.

You could either setup a config file that has 10 databases pre-defined with PDO and then you could overwrite the settings or save to one of the config files that have no settings assigned.

Sign up to request clarification or add additional context in comments.

4 Comments

ok, i understand that. It is better to add the connection in a file manually. It is possible to list this connections in a dropdown at login screen? I need that user after put your access data select the database he want and then the application use this database. How can i do that? @Birdy
Yes you could populate a select option manyally with lets say 10 pre-defined configuration layouts and in the config files you can store the values as variables then pass the data to the config file (Class) so then your database is dynamically stored, The other option would be to have a database table that stores your database values for each user or for each config you wish to have and then you just have 1 database connection class that retreieves the connection values from the database depending on the user id, would you like me to provide an example Without knowing your desired use case?
Yes, it is fantastic if you give me an exemple. What i want to do is: i have several databases(clients) and in my application login i have to select the database that i want to use, and after the login the application will load data of the selected database. Do you understand my ideia? @Birdy
I can do all my queries with Schema::connection($db).... this $db should be a global variable that user have selected... How can i put this variable in session for exemple?
1

You can use the config helper and set the config dynamically:

config([
    'database.connections.mysql.database' => $dynamicDB,
    'database.connections.mysql.username' => $dbUsername,
    'database.connections.mysql.password' => $dbPassword,
    // Any other dynamically set variables
]);

You could store the parameters in the session and default them to your config file:

config([
    'database.connections.mysql.database' => session('db_database', config('database.connections.mysql.database')),
    'database.connections.mysql.username' => session('db_username', config('database.connections.mysql.username')),
    'database.connections.mysql.password' => session('db_password', config('database.connections.mysql.password')),
    // Any other dynamically set variables
]);

Doing this in middleware would give you a little more control over where/when these parameters are being set and used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.