When you need multiple database connection in Laravel PHP then you can use DB
facade via connection
method.
In other way you can simply setup another database connection in config/database.php
and then you create protected variable for connection like this protected $connection = 'mysql_2';
Here is a simple step to use multiple database connection in Laravel.
Setup multiple databaseCreate another database connection by this way :
- 'mysql' => array(
- 'driver' => 'mysql',
- 'host' => 'localhost',
- 'database' => 'xyz',
- 'username' => 'root',
- 'password' => '',
- 'charset' => 'utf8',
- 'collation' => 'utf8_unicode_ci',
- 'prefix' => '',
- ),
- 'mysql1' => array(
- 'driver' => 'mysql',
- 'host' => 'localhost',
- 'database' => 'abc',
- 'username' => 'root',
- 'password' => '',
- 'charset' => 'utf8',
- 'collation' => 'utf8_unicode_ci',
- 'prefix' => '',
- ),
Now as per need create database connection variable in your model.
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- class Product extends Model
- {
- protected $connection = 'mysql1';
- }