0

I can set 2 different databases in config\database.php with

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

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

And in the model with

protected $connection = '1database';

protected $connection = '2database';

However, I want to use one controller and insert a conditional something like below

<?php

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

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

Unfortunately, this solution does not work.

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

Is it possible to do something like I want?

4 Answers 4

1

Import both of them. And then just initiate the correct one within the if statement. Something like this:

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , this solution is ok but I have to change all the models and all the calls inside the controller. It is ok, if I dont have other solution , is a solution.
1

You can implement trait for this purpose. And use it in your models.

<?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';
        }
    }
}

1 Comment

I think this is the cleanest option. You then can add use UsesTenantConnection; in your model
0

You can set the default DB connection inside \App\Providers\AppServiceProvider::boot() method.

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

1 Comment

thank you but I only have to use diferent database in some models
0

You can change the config database file with a helper function

1 - create helper function changeDatabases($database)

2 - change you config database

 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 - you must create two different model if they are not equal

the change is good the duration of the session

1 Comment

thank you but I only have to use diferent database in some models

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.