Laravel 支援多資料庫存取設定

Laravel 5.5 (或以上) 設定多資料庫設定需要處理幾個地方。

  1. .env的部分可以把database的相關全部註解掉(欸

  2. 在config/database.php裡面,先設定要連的db有幾個:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    connections' => [

    'db1' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'db1'),
    'username' => env('DB_USERNAME', 'db1'),
    'password' => env('DB_PASSWORD', 'db1'),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => 'InnoDB',
    ],

    'db2' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'db2'),
    'username' => env('DB_USERNAME', 'db2'),
    'password' => env('DB_PASSWORD', 'db2'),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => 'InnoDB',
    ],
    'db3' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'db3'),
    'username' => env('DB_USERNAME', 'abc'),
    'password' => env('DB_PASSWORD', 'abc'),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => 'InnoDB',
    ],


    ]
  3. 在config/database.php中設定預設會連線的database是哪一個:

    1
    'default' => 'db1',

    原則上這樣的處理方式就可以使用了。不過如果是設定利用Eloquent撈資料的話,要注意一下在建立model後要先做一些設定:

1
2
3
4
5
6
7
class Room extends Model
{
private $connection = 'db1';

protected $table = "table1";
protected $fillable = [];
}

這樣就可以在使用Eloquent的時候連到對的table上了。

Author

Walile

Posted on

2018-01-22

Updated on

2023-11-23

Licensed under