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:上面用了数组的省略写法