怎么在laravel数据库.php文件中动态添加连接
本教程将介绍如何在laravel数据库.php文件中动态添加连接的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
我需要用PHP代码把连接动态追加到lobase.php文件中,行不行?
推荐答案
我正在使用它
当我创建新客户时
public function setConnection($tenantName){
//GET Database Connection file path
$path = config_path('database.php');
//GET Database Connection file
$arr = include $path;
// load the array from the file
$new_connection=[
'driver' => 'mysql',
'host'=> env('DB_HOST'),
'database' => $tenantName,
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset'=> 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
];
// modify the array
$arr['connections'][$tenantName]=$new_connection;
// write it back to the file
file_put_contents($path, "<?php return " . var_export($arr, true) . ";");
}
好了关于怎么在laravel数据库.php文件中动态添加连接的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。