I have a project that must to be accessed through these 2 URLs us_myproject.com and es_myproject.com.
They share the same folders and code. In fact there are only one laravel project: htdocs/myproject.
depending on URL the information must to come from us_db or es_db, based on the URL used.
So my question is how to set .env db credentials to point to these 2 different databases. I have 2 different databases, 2 different user and 2 different password. How can I accomplish this task?
I have this follow code that works:
URL: us_myproject.com/dbtest
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=us_db
DB_USERNAME=root
DB_PASSWORD=''
Route::get('dbtest',function(){//Works good
$tblusertypes=DB::table("tblusertypes")->get();
return $tblusertypes;
});
If I do URL: es_myproject.com/dbtest should query against es_db but I dont know how to set this. Any Idea?
Reading your comments on an other answer, I have come up with the following solution:
<?php return [ ... 'connections' => [ ... 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ], ... ], ... ];<?php $config = [ ... 'connections' => [ ... 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ], ... ], ... ]; if( Request::server('HTTP_HOST') == 'es_myproject.com' ) { $config['connections']['mysql'] = [ 'driver' => 'mysql', 'host' => 'esHOST', 'database' => 'esDB', 'username' => 'esUSER', 'password' => 'esPASSWORD', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ]; } return $config;Here I assume that the
.envfile contains the database credentials for us_myproject.com.