Well this is possible in laravel.
1º You must configure your file in config > database.php
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'yoursql' => [
'driver' => 'mysql',
'host' => env('YOURSQL_HOST', '127.0.0.1'),
'port' => env('YOURSQL_PORT', '3306'),
'database' => env('YOURSQL_DATABASE', 'forge'),
'username' => env('YOURSQL_USERNAME', 'forge'),
'password' => env('YOURSQL_PASSWORD', ''),
'unix_socket' => env('YOURSQL_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
Once both databases are configured, if you notice they have 2 different names.
-mysql
-yoursql
Remember to configure in the ENV file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=base_de_datos_principal
DB_USERNAME=usuario_base_datos_principal
DB_PASSWORD=pass_usuario_base_datos_principal
YOURSQL_CONNECTION=mysql
YOURSQL_HOST=127.0.0.1
YOURSQL_PORT=3306
YOURSQL_DATABASE=base_de_datos_secundaria
YOURSQL_USERNAME=usuario_base_datos_secundaria
YOURSQL_PASSWORD=pass_usuario_base_datos_secundaria
Once the databases have been configured, you should bear in mind that:
-When you do not specify to laravel which connection you should use, by default you will use the first "mysql" database
Now you just have to specify the connection
::connection('yoursql')->where()....
Also, if a model were to use practically only the 2nd database, it is more practical to indicate to the model which connection should be made, than to specify the database in each query.
This you can do in the model
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class TUMODELO extends Model {
// Conexion que va a usar este modelo
protected $connection = 'yoursql';
...
}
Now, simply specify the connection to extract the data.
And then you specify the connection to upload the data. "voila", multiple connection.