0

How can i connect second database in model?

.env file

DB_HOST=localhost
DB_DATABASE=crmgen_lara1
DB_USERNAME=crmgen_lara1
DB_PASSWORD=Y.vfdsf

DB_HOST=localhost
DB_DATABASE=crmgen_restorant
DB_USERNAME=crmgen_restorant
DB_PASSWORD=@X(dsfs}

Database.php

 'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'crmgen_lara1'),
            'username'  => env('DB_USERNAME', 'crmgen_lara1'),
            'password'  => env('DB_PASSWORD', 'Y.ıquwewqe'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],

        'mysql2' => [
            'driver'   => 'mysql',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'crmgen_restorant'),
            'username' => env('DB_USERNAME', 'crmgen_restorant'),
            'password' => env('DB_PASSWORD', '@X(sadsa}'),
            'charset'  => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],

TestModel.php

$testt = new TestModel();
    $testt= TestModel::on('mysql2')->get();  

          dd($testt);
          die();

ERROR: Table 'crmgen_lara1.test' doesn't exist (SQL: select * from test) Because I can not connect second database. How can I solve this problem?

  • 1
    Does this answer your question? [How to use multiple databases in Laravel](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) – Kamlesh Paul Oct 07 '20 at 08:32

1 Answers1

0

You just have to differentiate the different keys of the .env, because the values ​​replace each other. DB_HOST, DB_HOST2 etc...

Noweh
  • 583
  • 5
  • 16