laravel 如何通过数据库连接多数据库获取数据
1个回答
2018-08-14 · 百度认证:云南新华电脑职业培训学校官方账号
云南新华电脑学校
云南新华电脑学校是经云南省教育厅批准成立的省(部)级重点计算机专业学校,采用三元化管理模式,教学设备先进,师资雄厚学生毕业即就业,学院引进了电商企业入驻,创建心为电商创业园区,实现在校即创业
向TA提问
关注
展开全部
在datebase.php文件中
'mysql' => [ 'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', ''), 'username' => env('DB_USERNAME', ''), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => 'wx_', 'strict' => false, 'engine' => null,
],'mysql_server' => [ 'driver' => 'mysql', 'host' => env('DB_HOST_SERVER', 'localhost'), 'port' => env('DB_PORT_SERVER', '3306'), 'database' => env('DB_DATABASE_SERVER', ''), 'username' => env('DB_USERNAME_SERVER', ''), 'password' => env('DB_PASSWORD_SERVER', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => 'wxs_', 'strict' => false, 'engine' => null,
],12345678910111213141516171819202122232425262728
在.env配置中
DB_CONNECTION=mysqlDB_HOST=localhostDB_PORT=3306DB_DATABASE=DB_USERNAME=DB_PASSWORD=DB_HOST_SERVER=localhostDB_PORT_SERVER=3306DB_DATABASE_SERVER=DB_USERNAME_SERVER=DB_PASSWORD_SERVER=123456789101112
在model文件中使用:
//第一种namespace App\Http\Models\Test;use DB;use Illuminate\Database\Eloquent\Model;class ispTest extends Model{
protected $connection = 'mysql_server'; private $table_name = 'users'; function serip(){
$res = DB::table($this->table_name)->connection($this->connection)->where('regip','=','0.0.0.0')->get(); return $res->toArray();
}
}?>//第二种namespace App\Http\Models\Test;use DB;use Illuminate\Database\Eloquent\Model;class ispTest extends Model{
protected $connection = 'mysql';//可以加可以不加
private $table_name = 'users'; function serip(){
$res = DB::table($this->table_name)->where('regip','=','0.0.0.0')->get(); return $res->toArray();
}
}123456789101112131415161718192021222324252627282930313233
'mysql' => [ 'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', ''), 'username' => env('DB_USERNAME', ''), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => 'wx_', 'strict' => false, 'engine' => null,
],'mysql_server' => [ 'driver' => 'mysql', 'host' => env('DB_HOST_SERVER', 'localhost'), 'port' => env('DB_PORT_SERVER', '3306'), 'database' => env('DB_DATABASE_SERVER', ''), 'username' => env('DB_USERNAME_SERVER', ''), 'password' => env('DB_PASSWORD_SERVER', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => 'wxs_', 'strict' => false, 'engine' => null,
],12345678910111213141516171819202122232425262728
在.env配置中
DB_CONNECTION=mysqlDB_HOST=localhostDB_PORT=3306DB_DATABASE=DB_USERNAME=DB_PASSWORD=DB_HOST_SERVER=localhostDB_PORT_SERVER=3306DB_DATABASE_SERVER=DB_USERNAME_SERVER=DB_PASSWORD_SERVER=123456789101112
在model文件中使用:
//第一种namespace App\Http\Models\Test;use DB;use Illuminate\Database\Eloquent\Model;class ispTest extends Model{
protected $connection = 'mysql_server'; private $table_name = 'users'; function serip(){
$res = DB::table($this->table_name)->connection($this->connection)->where('regip','=','0.0.0.0')->get(); return $res->toArray();
}
}?>//第二种namespace App\Http\Models\Test;use DB;use Illuminate\Database\Eloquent\Model;class ispTest extends Model{
protected $connection = 'mysql';//可以加可以不加
private $table_name = 'users'; function serip(){
$res = DB::table($this->table_name)->where('regip','=','0.0.0.0')->get(); return $res->toArray();
}
}123456789101112131415161718192021222324252627282930313233
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询