只需三步,便可實現。
第一步,在.env文件中配置
DB_HOST=localhost DB_DATABASE=test DB_USERNAME=root DB_PASSWORD=root DB_HOST_TWO=localhost DB_DATABASE_TWO=test1 DB_USERNAME_TWO=root DB_PASSWORD_TWO=root
第二步,在config/database.php進行數據庫配置
//系統默認優先使用第一個
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '192.168.0.y'),
'database' => env('DB_DATABASE', 'yyy'),
'username' => env('DB_USERNAME', 'yyyy'),
'password' => env('DB_PASSWORD', 'yyyyy'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql_two' => [
'driver' => 'mysql',
'host' => env('DB_HOST_TWO', '192.168.0.x'),
'database' => env('DB_DATABASE_TWO', 'xxx'),
'username' => env('DB_USERNAME_TWO', 'xxxx'),
'password' => env('DB_PASSWORD_TWO', 'xxxxx'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
第三步,在你需要用到的model中,進行切換
class meeting_news extends Model
{
//沒有指定的話,默認使用 mysql
protected $connection = 'mysql_two';
protected $table = 'meeting';
}
至於上面的xxx什么yyy的,線上服務自己配一下就行了
