Laravel在运行时更改数据库参数 [英] Laravel change database parameters at run time

查看:99
本文介绍了Laravel在运行时更改数据库参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户登录后在运行时更改数据库连接.我使用Config Facade的方法集.我知道我只能在中间件或控制器构造函数上使用它.所以我创建了这些

I want change in run time the database connection after user login. I use the method set of Config Facade. I know that I can use it only on Middleware or Controller constructor. So I created these

 Route::get("login", "Login_Controller@login");

 Route::group(["middleware" => "test"], function() {
    Route::post("login", "Login_Controller@login");
 });

然后我创建了登录提交后调用的测试"中间件:

And then I created the "Test" Middleware called after login submit:

public function handle($request, Closure $next) {

    // Validazione dei dati
    $validator = Validator::make($request->all(), [
                "codice_azienda" => "required",
                "username" => "required",
                "password" => "required"
    ]);

    if ($validator->fails()) {
        return redirect()->back()->withInput()->withErrors($validator);
    }

    // Verifico i dati immessi
    $codice_azienda = $request->get("codice_azienda");
    $username = $request->get("username");
    $password = $request->get("password");

    $objOperatore = new Operatore();
    $cliente = $objOperatore->loginOperatore($codice_azienda, $username, $password);
    if (empty($cliente)) {
        throw new \App\Exceptions\LoginFailedException;
    }

    Config::set("DB_HOST", Crypt::decrypt($cliente->Server));
    Config::set("DB_DATABASE", $cliente->NomeDB);
    Config::set("DB_USERNAME", Crypt::decrypt($cliente->Username));
    Config::set("DB_PASSWORD", Crypt::decrypt($cliente->Password));

    $operatori = Operatore_Model::all();
    \App\Http\Controllers\Log_Controller::debug($operatori, true);

    return $next($request);
}

但是Operatore_Model的metoed all()不返回任何内容

But the metoed all() of Operatore_Model doesn't return anything

日志返回此错误:

"[2017-02-16 16:44:52] local.ERROR:异常'PDOException'与 消息'SQLSTATE [42S02]:[Microsoft] [SQL的ODBC驱动程序11 服务器] [SQL Server]无效的对象名称"Operatori".在 C:\ xampp \ htdocs \ dashboard \ www \ e730 \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Connection.php:323"

"[2017-02-16 16:44:52] local.ERROR: exception 'PDOException' with message 'SQLSTATE[42S02]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid object name 'Operatori'.' in C:\xampp\htdocs\dashboard\www\e730\vendor\laravel\framework\src\Illuminate\Database\Connection.php:323"

我想使用一个多数据库连接,每个客户一个.

I want use a multi DB connection, one for each customer.

推荐答案

我以这种方式解决:

config :: set的一部分是错误的,要访问数据库配置,我应该使用点(.)样式,

The part of config::set was wrong, to access database configuration, I should use the dot (.) style, in this way:

Config::set("database.connections.sqlsrv.host", Crypt::decrypt($cliente->Server));
Config::set("database.connections.sqlsrv.database", $cliente->NomeDB);
Config::set("database.connections.sqlsrv.username", Crypt::decrypt($cliente->Username));
Config::set("database.connections.sqlsrv.password", Crypt::decrypt($cliente->Password));

然后重新连接到数据库:

And then reconnect to DB:

\Illuminate\Support\Facades\DB::reconnect();

对于所有有此问题的人,我的建议是使用双数据库连接,一个用于主数据库(服务器数据库),一个用于客户数据库.这样,您可以使用以下简单代码切换到两个数据库:

My advise for all that have this problem, is use a double db connection, one for the main db (server db) and one for customer db. In this way you can switch to both db, with this simple code:

Config::set("database.default", "sqlsrvCustomer");
\Illuminate\Support\Facades\DB::reconnect();

使用第一个命令,您可以选择客户数据库,使用第二个命令,您可以连接到该数据库.

With the first command you can choose the customer DB and with the second you can connect to it.

这篇关于Laravel在运行时更改数据库参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆