1. 程式人生 > 實用技巧 >DB 如何自定義資料庫連線

DB 如何自定義資料庫連線

What you can do is kind of half measure:

Config::set('database.connections.foo', [
    'driver' => 'mysql',
    'host' => 'localhost',
    'port' => '3306',
    'database' => 'foo_db',
    'username' => 'root',
    'password' => 'secret',
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null,
]);

$result = DB::connection('foo')->select('show tables'));

延伸理解

1)laravel 的所以配置檔案都在根目錄下的 config 目錄裡,直接看一個配置檔案的名字就知道是做什麼的了,這裡不說了

2)讀取配置的方法

$value = config('app.timezone');

即使用內建的config函式+配置檔名+配置引數名方式來讀取配置的值

3)設定配置的方法(只是在執行時(runtime)配置,不會寫入到配置檔案裡)

config(['app.timezone' => 'Asia/Shanghai']);

PS:上面用了陣列的省略寫法