【问题标题】:Laravel 5.5, 2 different databases and different modelsLaravel 5.5,2个不同的数据库和不同的模型
【发布时间】:2018-08-25 17:21:40
【问题描述】:

我可以在 config\database.php 中设置 2 个不同的数据库

'conection' => ['database01' => [ ...

'conection' => ['database02' => [ ...

模型中与

protected $connection = '1database';

protected $connection = '2database';

但是,我想使用一个 控制器 并插入一个类似下面的条件

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

if (\Session::get('db')=='database01'){
    use App\Model01;
}else{
    use App\Model02;
}

很遗憾,此解决方案不起作用。

public function index()
    {
    if ($baseDat1){
         $data= Data01::orderBy('id', 'desc')->take(25)->get();
    }else{
        $data= Data02::orderBy('id', 'desc')->take(25)->get();
    }        
}

有没有可能做我想做的事?

【问题讨论】:

    标签: php laravel database laravel-5 eloquent


    【解决方案1】:

    您可以在 \App\Providers\AppServiceProvider::boot() 方法中设置默认数据库连接。

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if (true) {
            $this->app['db']->setDefaultConnection('database_01');
        } else {
            $this->app['db']->setDefaultConnection('database_02');
        }
    }
    

    【讨论】:

    • 谢谢,但我只需要在某些模型中使用不同的数据库
    【解决方案2】:

    您可以使用辅助函数更改配置数据库文件

    1 - 创建辅助函数changeDatabases($database)

    2 - 改变你的配置数据库

     if(database == 'baseDat1'){
     config(['database.connections.mysql.host' => 'newHost']);
     config(['database.connections.mysql.database' => 'newDatabase']);
     config(['database.connections.mysql.username' => 'newUsername']);
     config(['database.connections.mysql.password' => 'newPassword']);
    }
    

    3 - 如果它们不相等,则必须创建两个不同的模型

    变化是好的会话持续时间

    【讨论】:

    • 谢谢,但我只需要在某些模型中使用不同的数据库
    【解决方案3】:

    同时导入它们。然后在if 语句中启动正确的一个。像这样的:

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Model01;
    use App\Model02;
    // ...
    
    // Your controller
    if (\Session::get('db')=='database01'){
        $model = app()->make(Model01::class);
    } else {
        $model = app()->make(Model02::class);
    }
    // And now you can make queries with your $model like this
    $results = $model->where(condition)->get();
    

    【讨论】:

    • 谢谢,这个解决方案没问题,但我必须更改控制器内的所有模型和所有调用。没关系,如果我没有其他解决方案,就是解决方案。
    【解决方案4】:

    您可以为此目的实现 trait。并在您的模型中使用它。

    <?php
    
    namespace App;
    
    trait UsesTenantConnection
    {
        /**
         * Get the current connection name for the model.
         *
         * @return string
         */
        public function getConnectionName()
        {
            if (true) { //your condition here
                return 'database_01';
            } else {
                return 'database_02';
            }
        }
    }
    

    【讨论】:

    • 我认为这是最干净的选择。然后你可以在你的模型中添加use UsesTenantConnection;
    猜你喜欢
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2020-07-17
    • 1970-01-01
    • 2012-04-11
    相关资源
    最近更新 更多