laravel 连接多个数据库怎么使用
1个回答
展开全部
laravel使用多个数据库连接
1、配置.env文件
1 DB_CONNECTION=mysql
2 DB_HOST=127.0.0.1
3 DB_PORT=3306
4 DB_DATABASE=database_name
5 DB_USERNAME=root
6 DB_PASSWORD=
7
8 DB_HOST_CENTER=127.0.0.1
9 DB_PORT_CENTER=3306
10 DB_DATABASE_CENTER=database_center
11 DB_USERNAME_CENTER=root
12 DB_PASSWORD_CENTER=
2、配置config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_center' => [
'driver' => 'mysql',
'host' => env('DB_HOST_CENTER', 'localhost'),
'port' => env('DB_PORT_CENTER', '3306'),
'database' => env('DB_DATABASE_CENTER', 'forge'),
'username' => env('DB_USERNAME_CENTER', 'forge'),
'password' => env('DB_PASSWORD_CENTER', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
3、创建model
// 这个model将采用默认的'mysql'连接
class UserModel extends Model
{
// 数据库'database'中的users表
protected $table = "users";
}
// 这个model将使用mysql_center连接
class UserModel extends Model
{
// 数据库'dadtabase_center'中的users表
protected $connection = 'mysql_center';
protected $table = "users";
}
1、配置.env文件
1 DB_CONNECTION=mysql
2 DB_HOST=127.0.0.1
3 DB_PORT=3306
4 DB_DATABASE=database_name
5 DB_USERNAME=root
6 DB_PASSWORD=
7
8 DB_HOST_CENTER=127.0.0.1
9 DB_PORT_CENTER=3306
10 DB_DATABASE_CENTER=database_center
11 DB_USERNAME_CENTER=root
12 DB_PASSWORD_CENTER=
2、配置config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_center' => [
'driver' => 'mysql',
'host' => env('DB_HOST_CENTER', 'localhost'),
'port' => env('DB_PORT_CENTER', '3306'),
'database' => env('DB_DATABASE_CENTER', 'forge'),
'username' => env('DB_USERNAME_CENTER', 'forge'),
'password' => env('DB_PASSWORD_CENTER', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
3、创建model
// 这个model将采用默认的'mysql'连接
class UserModel extends Model
{
// 数据库'database'中的users表
protected $table = "users";
}
// 这个model将使用mysql_center连接
class UserModel extends Model
{
// 数据库'dadtabase_center'中的users表
protected $connection = 'mysql_center';
protected $table = "users";
}
网易云信
2023-12-06 广告
2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询