Changing laravel database connection time

I need to change laravel 5 database connection at runtime.

Do you have any ideas?

Please share with me.

Thanks.

+11
source share
4 answers

Update : this answer is from 2015! I have not used Laravel for many years, and I am not up to date with the latest best practices. They voted for this answer, so I think it works, but please proceed with caution.


Well, my immediate answer to this is: no. Most likely, you can accomplish your task by changing the data model and working with more complex relationships. It's hard to say without knowing what you want to do, but it seems to me that this is a bad idea in general, especially if you plan to use eloquent models, etc.

However, in some scenario, when you really need to change the data in another database or execute some raw query, you can use the DB::connection() method. Sort of:

 $data = DB::connection('another_connection')->select(...); 

You can specify this another_connection variable in your database.php file. Like this:

 <?php return array( 'default' => 'mysql', 'connections' => array( # Your regular connection 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'user', 'password' => 'password' 'charset' => 'utf8', ), # Your new connection 'another_connection' => array( 'driver' => 'mysql', 'host' => 'another_host', 'database' => 'another_db', 'username' => 'user1', 'password' => 'password1' 'charset' => 'utf8', ), ), ); 

You can even specify a connection for each eloquent model using protected $connection = 'another_connection'; also you can specify a connection for each model instance created / requested at runtime

 $user = new User; $user->setConnection('another_connection'); $user1 = $user->find(1); 

But then again, I personally do not think this is a good idea, and it seems to me that everything can get confused and fall apart very quickly, as your application gets complicated.

+13
source

You can change connection data at runtime using

 Config::set('database.connections.mysql.database', 'other_database'); 

but do not forget to clear before deleting the cache

 DB::purge('mysql'); 
+15
source

We have a similar scenario where to look for historical data, we are changing the database. In the model, we use the following:

 public function inventory($db, $id) { return DB::table($db . '.inventory')->where('inventoryid', $id)->get(); } 

and then, in code, we send $ db as a parameter:

 $inventory = Inventory::inventory('data_2016', 2); 

Thus, you do not need to define a connection for each of the different databases and do not redo the database configuration files.

The only drawback is that you cannot directly use the model, instead you need to use the DB helper.

+1
source

You can change this since Laravel 5.x or 6.x

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

Where databasename should be specified in config/database in the connections array.

+1
source

Source: https://habr.com/ru/post/989709/


All Articles