作为程序员一定要保持良好的睡眠,才能好编程

laravel中如何定义多个数据库连接、Model如何选择数据库

发布时间:2019-07-06

在一个项目中,有时候需要连接多个数据库


laravel 是否可以连接多个数据库? laravel 数据库是如何设置  如何使用的?


首先找到  config/database.php  这个文件

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'bhc_region_c'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

     'connections' => [
        'region_c' => [
            'driver' => 'mysql',
            'write' => [
                'host' => env('REGION_C_DB_HOST', '127.0.0.1'),
                'username' => env('REGION_C_DB_USERNAME', 'root'),
                'password' => env('REGION_C_DB_PASSWORD'),
                'port' => env('REGION_C_DB_PORT', '3306'),
            ],
            'read' => [
                'host' => env('SLAVE_REGION_C_DB_HOST', '127.0.0.1'),
                'username' => env('SLAVE_REGION_C_DB_USERNAME', 'root'),
                'password' => env('SLAVE_REGION_C_DB_PASSWORD'),
                'port' => env('SLAVE_REGION_C_DB_PORT', '3306'),
            ],
            'database' => env('REGION_C_DB_DATABASE'),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
            'options'=> [
	            PDO::ATTR_EMULATE_PREPARES => true
            ],
        ],
        'anhao_four' => [
            'driver' => 'mysql',
            'host' => env('ANHAO_FOUR_DB_HOST', '127.0.0.1'),
            'port' => env('ANHAO_FOUR_DB_PORT', '3306'),
            'database' => env('ANHAO_FOUR_DB_DATABASE', 'forge'),
            'username' => env('ANHAO_FOUR_DB_USERNAME', 'forge'),
            'password' => env('ANHAO_FOUR_DB_PASSWORD', ''),
            'unix_socket' => env('ANHAO_FOUR_DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
            'options'=> [
	            PDO::ATTR_EMULATE_PREPARES => true
            ],
        ]
    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */
     
   
   /**
    * 是否开启sql语句日志
    */
    'enableQueryLog' => env('ENABLE_QUERY_LOG', false),
];




namespace App\Models\BhcRegionC;

use Illuminate\Database\Eloquent\Model as BaseModel;

class Model extends BaseModel
{
    const CREATED_AT = 'gmt_created';
    const UPDATED_AT = 'gmt_modified';
    protected $connection = 'region_c';   // 连接 region_c  连接
}



class Model extends BaseModel
{
    const CREATED_AT = 'gmt_created';
    const UPDATED_AT = 'gmt_modified';
    protected $connection = 'anhao_four';   // 连接 region_c  连接
}