1

I am working on a new application that must connect to several databases. The databases are dynamically created when a new company is registered in my application i.e each company have there on databases.

My question is how to establish the database connection using laravel,

If the company admin is login he needs to connect the corresponding db. How is it possible ???

I know how to establish multiple database connection if the databases are stable, like,

'mysql1' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge1'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
...

'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge2'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
...

but if the databases are not stable how is it possible ????

If my question is not up to standard, please forgive me

2 Answers 2

1

You can add dynamically the database configuration like this.

if(NULL === config()->get('database.connections.company')){
        config()->set('database.connections.company', [
            'driver'    => $companyDbDriver,
            'host'      => $companyDbHost,
            'port'      => $companyDbPort,
            'database'  => $companyDbName,
            'username'  => $companyDbUser,
            'password'  => $companyDbPassword,
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false
        ]);
    }

in the model; set the connection attribute to 'company'

/**
 * Connection configuration ID.
 *
 * @var string
 */
protected $connection = 'company';

there are complication if you dont load the configuration of the database before calling the models using it, i'll leave that to you.

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

Comments

1

First you have to know where your database connections parameters are stored. Anyway, wherever they are, you should create a new connection on the fly. Supposing each User has it's own connection ...

<?php
use Illuminate\Support\Facades\Config;

$user = Users::find($id);
Config::set('database.connections.' . $user->username, array(
        'driver'    => 'mysql',
        'host'      => $user->db_host,
        'database'  => $user->db_name,
        'username'  => $user->db_user,
        'password'  => $user->db_pass,
            'charset'   => 'utf8',
        'collation' => 'utf8_general_ci',
        'prefix'    => '',
));
// And set the new connection to interested models
$myModel = new MyModel;
$myModel->setConnection($user->username);
$data = $myModel->where(...)->get();

Hope this help

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.