How to Use Multiple Database Connections in Laravel Application

How to Use Multiple Database Connections in Laravel Application

In this post we will give you information about How to Use Multiple Database Connections in Laravel Application. Hear we will give you detail about How to Use Multiple Database Connections in Laravel ApplicationAnd how to use it also give you demo for it if it is necessary.

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 database

Create another database connection by this way :

  1.         'mysql'=>array(
  2.             'driver'=>'mysql',
  3.             'host'=>'localhost',
  4.             'database'=>'xyz',
  5.             'username'=>'root',
  6.             'password'=>'',
  7.             'charset'=>'utf8',
  8.             'collation'=>'utf8_unicode_ci',
  9.             'prefix'=>'',
  10.         ),
  11.         'mysql1'=>array(
  12.             'driver'=>'mysql',
  13.             'host'=>'localhost',
  14.             'database'=>'abc',
  15.             'username'=>'root',
  16.             'password'=>'',
  17.             'charset'=>'utf8',
  18.             'collation'=>'utf8_unicode_ci',
  19.             'prefix'=>'',
  20.         ),
		'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.

  1. <?php
  2. namespace App;
  3. use IlluminateDatabaseEloquentModel;
  4. class Product extends Model
  5. {
  6. protected $connection='mysql1';
  7. }
<?phpnamespace App;use IlluminateDatabaseEloquentModel;class Product extends Model{ protected $connection = 'mysql1';}

As i create connection variable in Product model then it tells that product model is using second database connection.

Here you can also use config helper function to set dynamically database connection or change database config at runtime.

Click here to know more How to set and get config variable in Laravel 5

Hope this code and post will helped you for implement How to Use Multiple Database Connections in Laravel Application. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

6  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US