Laravel框架實現同時連線多個數據庫
阿新 • • 發佈:2018-11-07
laravel4.2版本,
1.修改配置檔案:config/database.php
'pgsql' => array( 'driver' => 'pgsql', 'host' => '10.0.2.203', 'database' => 'statist', 'username' => 'immuser', 'password' => 'immuser', 'charset' => 'utf8', 'prefix' => '', 'schema' => 'statist', ), 'newpro' => array( 'driver' => 'pgsql', 'host' => '10.0.2.203', 'database' => 'newpro', 'username' => 'immuser', 'password' => 'immuser', 'charset' => 'utf8', 'prefix' => '', 'schema'=> 'newpro', ),
2.查詢資料過程連線newpro資料庫,預設情況連線的是pgsql資料庫
你可能使用很多的資料庫系統,你可以使用 DB::connection
方法來選擇使用它們:
$users = DB::connection('foo')->select(...);
例項:
class DeviceActionAnalysis extends Eloquent{ protected $table = "devices"; public function getDeviceIllegal($organization=1,$name=null) { $result = DB::connection("newpro")->table($this->table) ->get(); var_dump($result); return $result; } }