laravel的多数据操作
2018-12-11 13:57:00 来源:admin 点击:855
第一步:修改.env文件
DB_CONNECTION=数据库类型
DB_HOST=111.111.111.111
DB_PORT=5432
DB_DATABASE=数据库名称
DB_USERNAME=用户名
DB_PASSWORD=密码
#下面是另一个数据库的配置
DB_HOST_CHAT=111.111.111.111
DB_PORT_CHAT=3306
DB_DATABASE_CHAT=数据库名称
DB_USERNAME_CHAT=用户名
DB_PASSWORD_CHAT=密码
第二步:修改database.php
'自定义命名' => [
'driver' => 'mysql',
'host' => env('DB_HOST_CHAT', '127.0.0.1'),
'port' => env('DB_PORT_CHAT', '3306'),
'database' => env('DB_DATABASE_CHAT', 'forge'),
'username' => env('DB_USERNAME_CHAT', 'forge'),
'password' => env('DB_PASSWORD_CHAT', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'默认命名' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
第三步:使用
$database = DB::connection('自定义命名');
$mine = $database->table('表名称')->where('id', 213123123)->first();
这个使用方法和正常查询没区别,下面是第二种方法
$mine = DB::connection('自定义命名')->select("select * from 表);
第四步:完事