Laravel/Laravel-admin 使用多个数据库连接
Posted 搬砖小伙子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel/Laravel-admin 使用多个数据库连接相关的知识,希望对你有一定的参考价值。
1、配置.env 文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yhadmin
DB_USERNAME=root
DB_PASSWORD=root
DB_HOST_CENTER=127.0.0.1
DB_PORT_CENTER=3306
DB_DATABASE_CENTER=yhadmin_sub
DB_USERNAME_CENTER=root
DB_PASSWORD_CENTER=root
DB_PREFIX=yh_
2、配置 config/database.php
\'mysql\' => [
\'driver\' => \'mysql\',
\'host\' => env(\'DB_HOST\', \'127.0.0.1\'),
\'port\' => env(\'DB_PORT\', \'3306\'),
\'database\' => env(\'DB_DATABASE\', \'forge\'),
\'username\' => env(\'DB_USERNAME\', \'forge\'),
\'password\' => env(\'DB_PASSWORD\', \'\'),
\'unix_socket\' => env(\'DB_SOCKET\', \'\'),
\'charset\' => \'utf8mb4\',
\'collation\' => \'utf8mb4_unicode_ci\',
\'prefix\' => \'\',
\'strict\' => true,
\'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\' => env(\'DB_PREFIX\', \'\'), //数据库前缀
\'strict\' => false,
\'engine\' => null,
],
3、创建 model
a、这个model将采用默认的\'mysql\'连接
class UserModel extends Model
{
// 数据库\'database\'中的users表
protected $table = "users";
}
b、 这个model将使用mysql_center连接
class UserModel extends Model
{
// 数据库\'dadtabase_center\'中的users表
protected $connection = \'mysql_center\';
protected $table = "users";
}
----------------------------------------------laravel-admin中-----------------------------------------------------------------
以上是关于Laravel/Laravel-admin 使用多个数据库连接的主要内容,如果未能解决你的问题,请参考以下文章
windows环境下composer安装laravel,laravel-admin,新手教程
windows环境下composer安装laravel,laravel-admin,新手教程