简体   繁体   中英

Connect to dynamic database for one model in laravel

I want to connect second database in Laravel for one model. The second database configuration pass from the controller. Please help me to develop this part.

This is my controller

$userdata=App\User::get_userdata($dbname,$dbuser,$dbpassword)

This is my model

public function get_userdata($dbname,$dbuser,$dbpassword){
$query=DB::connection(???)->table('users')->first(); 
return $query; 
}

Can I try with this? Or have any other way to do this for me.

I solved it

        config::set(['database.connections.userdb'=>[
          'driver' => 'mysql',
          'url' => env('DATABASE_URL'),
          'host' => env('DB_HOST','127.0.0.1'),
          'port' => env('DB_PORT','3306'),
          'database' => 'ABC',
          'username' => 'ABC',
          'password' => 'ABC'
        ]]);

DB::connection('userdb')->table('usermas')

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