Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel Change Database connection run time

Tags:

I need to change laravel 5 database connection in run time.

Do you have any idea about it?

Please share with me.

Thanks.

like image 313
Arjun Avatar asked Jun 25 '15 05:06

Arjun


People also ask

How do I change the database connection in Laravel runtime?

Forum change db at runtime connections. mysql", [ "host" => "localhost", "driver" => "mysql", "default" => "mysql", "database" => $dati->db, "username" => "root", "password" => "" ]); DB::reconnect('mysql'); but it work only for the controller and model where i call the function.

How can check DB connection in Laravel?

Echo the Laravel database name in Blade/PHP This will output the name of the database or return 'none' if there is no connection. If you view it in the browser, it gives you the name of the connected database. Checking whether the application is connected to a Laravel database.

What is DB purge Laravel?

This usually happens when you forget to DB::purge(connection); . That's because Laravel will cache the information filled in the config/database. php file inside the DatabaseManager class, but the config starts empty. By purging it, we clean it from the empty information and let the new information kick in.

How does Laravel connect to database?

Laravel makes connecting with databases and running queries extremely simple. The database configuration file is app/config/database. php . In this file you may define all of your database connections, as well as specify which connection should be used by default.


2 Answers

You can change the connection details during run time using

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

but don't forget to run purge before in order to delete the cache

DB::purge('mysql'); 
like image 198
NiRR Avatar answered Sep 25 '22 11:09

NiRR


Update: This answer is from 2015! I haven't used Laravel in years and I am not up to date with the latest best practices. This answer keeps getting upvoted so I suppose it works but please proceed with caution.


Well, my immediate answer to this is: don't. Chances are that you can accomplish your task by changing your data model and working with some more advanced relationships. It's hard to tell without knowing what you want to do but it seems to me like a bad idea in general, specially if you're planning on using eloquent models and so on

That said, in some scenario where you really need to alter data in another database or execute some raw query you can use the DB::connection() method. Something like:

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

You can specify that another_connection variable at 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/queried at run time

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

But then again, I personally don't think this is a good idea and it seems to me that everything can get messy and fall apart very quickly as your application grows in complexity.

like image 26
FBidu Avatar answered Sep 25 '22 11:09

FBidu