In addition to the main base, there was a need to connect to an external one, which is located on another server. Tell me plz how can this be done?

    1 answer 1

    Here is the solution:

    1. Go to the config / database.php file and add a new connection to the array there:

      'mysql2' => [ 'driver' => 'mysql', 'host' => env('DB_HOST2', 'localhost'), 'database' => env('DB_DATABASE2', ''), 'username' => env('DB_USERNAME2', ''), 'password' => env('DB_PASSWORD2', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ], 
    2. In the .env file add access to this connection, that is DB_HOST2, DB_DATABASE2, DB_USERNAME2, DB_PASSWORD2.

    3. There are several ways to access this connection. Here is one of them:

       $users = DB::connection('mysql2')->select("select ...");