For Laravel > 5.3
- Add in .env file properties of the second connection (DB_HOST, DB_PORT, DB_DATABASE, DBUSERNAME, DB_PASSWORD)
- Add second connection in config/database.php, connection array. Example:
'mysql2' => [ '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', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => 'InnoDB ROW_FORMAT=DYNAMIC', ],
- Make a connection
use Illuminate\Support\Facades\DB; $results = DB::connection('mysql2')->select...
- Raw PDO event
$pdo = DB::connection('mysql2')->getPdo();
- Specific table query
$pdo = DB::connection('mysql2')->table('tableName')->select....;