简体   繁体   中英

How to change database name before authentication in laravel 5.1

I have multiple database. I want to change db name based on the url dynamically. How can I set particular db before authentication.

I want to change database from authentication to through out the application. For ex. If url is like lara.local.com/comapny1 then it will select database company1

If url is like lara.local.com/company2 then it will select database company2

Based on the selected database authentication will be done and selected database will be used for that user.

Make entry for second database in config/database.php

'company1' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'database1'),
    'username'  => env('DB_USERNAME', 'root'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
    ],

    'company2' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => 'database2',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
     ],

By default your queries will use the mysql connection to change connection to 'company1'

DB::connection('company1')->select($query);

Additionally you can set database connection for Model

$someModel = new MyModel;
$someModel->setConnection('company1');

You can use the Request::is() to get URI from URL

if(Request::is('company1')){
    //change database to company1
    Config::set("database.connections.company1.database", 'company1');
}
elseif(Request::is('company2'){
    Config::set("database.connections.company1.database", 'company2');
}

You can achieve this thing with below steps:

1 Define multiple connection into database.php for example:

'db1' => array(    
    'driver'    => 'mysql',  
    'host'      => 'localhost',  
    'database'  => 'db1_connection',  
    'username'  => 'root',  
    'password'  => '',  
    'charset'   => 'utf8',  
    'collation' => 'utf8_unicode_ci',  
    'prefix'    => '',  
),

2 Use connection based on conditional statement(You should move this code else)

//Check url that contains 'MYCODE'
if ('URL Contains' == 'MYCODE') {
  DB::disconnect();
  Config::set('database.default','db1');
  DB::reconnect();
}
else {
    DB::disconnect();
  Config::set('database.default','db2');
  DB::reconnect();
}

It should work for you, however I haven't tested it. Reference taken from answer by Marcin Nabialek

You can do this in AppServiceProvider's boot() like this

public function boot()
{
  if($this->app['request']->getHost()=='test.com') {
       Config::set('database.default','mysql');
    }
  else{
    Config::set('database.default','mysql1');
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM