Change the Database Connection Dynamically in Laravel

17,620

Solution 1

Well you can use the default database for user login and have a new field for the database name. Then whenever you need to query a different database, you can just change your db connection.

Something like this

$someModel = new SomeModel;
$databaseName = "mysql2"; // Dynamically get this value from db
$someModel->setConnection($databaseName);
$something = $someModel->find(1);

You can read more about it here. http://fideloper.com/laravel-multiple-database-connections

Solution 2

This way you can set new parameter when it comes to database:

 \Config::set('database.connections.mysql.database', $schemaName);

Remember about PURGE to persist this settings

 DB::purge('mysql');

Cheers!

Solution 3

you need to get the config first.. then alter the specific field then set it back..

$config = Config::get('database.connections.company');
$config['database'] = "company_tenant_$id";
$config['password'] = "test2123";
config()->set('database.connections.company', $config);

Solution 4

I think a good place to change the database connection place is in bootstrap/app.php file, use the code below:

$app->afterBootstrapping(\Illuminate\Foundation\Bootstrap\LoadConfiguration::class, function ($ap) {
    // your database connection change may happens here
});

It is before ServiceProvider register and boot, so even if you use DB or Eloquent staff in ServiceProvider, it works very well.

Share:
17,620
tyro
Author by

tyro

PHP Developer

Updated on July 18, 2022

Comments

  • tyro
    tyro almost 2 years

    I have the master database with login table and corresponding database settings for each user. On login I should dynamically change the db settings fetching from the table. I can change the db connection but this is not persisting.

    Config::set("database.connections.mysql", [
    'driver' => 'mysql',
    "host" => $usr_host,
    "database" => $usr_database,
    "username" => $usr_username,
    "password" => $usr_password,
    ...
    ]);
    

    edit: New database is created for each user when he/she registers with the app and thus i dont have the database connection for each user defined in the config/database.php

  • tyro
    tyro over 6 years
    this settings changes to default in next request
  • Joshy Francis
    Joshy Francis over 5 years
    purge function of DB class is very useful.
  • Alberto Acuña
    Alberto Acuña over 4 years
    what if im not working with model, and im using just ::table
  • Omer Farooq
    Omer Farooq over 4 years
    Well this isnt elegant and wont recommend it, but an easier way of doing this would be to somehow change the returned array in app/config/database.php. So you add some condition if the condition is met then you return secondary db details else you always return primary db details.
  • Noor Ahmed
    Noor Ahmed over 4 years
    But I need to check if the user is logged in then I want the use the dynamic db connection, else I want to use the default login database ( by which the user can only view the login page and nothing more )
  • Noor Ahmed
    Noor Ahmed over 4 years
    I just tried your method. I really like to to do it your way. But inside that code block when I want to check if the user is logged in, it says that Auth class is not defined. How can I access the Auth class inside that code block? If I am just able to check if the user is logged in, then I will be all set for this.
  • Noor Ahmed
    Noor Ahmed over 4 years
    where/which file will I have to do this? I am just lost. Please let me know the exact file path where I can do this. I just want to be able to do it once and then I should no longer have to do it every time in every class and container.
  • Mohamed Salah
    Mohamed Salah over 3 years
    Your answer is really helped me, thank you very much. there is a case were we need to change only the connection database name as we might have the same connection but with multiple dbs