How to use multiple databases in a single Laravel site

This is very important question we faced recently from our clients that “Can we use 2 database in a Laravel application?”

answer is YES.

Laravel provinding very flexible and robust solutions for developer. It provides options from which we can connect multiple databases for a single application.

There some specific settings need to be done to connect multiple databases in Laravel.

So let me give you step by step process. It’s mentioned below:

Step 1:
Add new database details in your project .env file. There are predefined constants to add database details like below:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=secret

You just need to copy and paste it to make another database details and rename all constants to provide a new name like below


DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=database2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=secret

Step 2:
Now you need to link this second database details to Laravel database configuration in config/database.php file. Below mentioned code available so you just need to copy and paste this to make another settings like below:


//----- This code will be already in config/database.php,

you need to copy and paste to make new one ----------------//


'mysql' => [
'driver' => env('DB_CONNECTION'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
]


//----- This is new setting for second database from .env file ----------------//
'mysql2' => [
'driver' => env('DB_CONNECTION_SECOND'),
'host' => env('DB_HOST_SECOND'),
'port' => env('DB_PORT_SECOND'),
'database' => env('DB_DATABASE_SECOND'),
'username' => env('DB_USERNAME_SECOND'),
'password' => env('DB_PASSWORD_SECOND'),
]

By following above two steps, we can connect two different database in our project. And if you are not providing database connection into your Laravel query then it will connect to default database (database 1) and if you provide database connection name then it will connect your called database like below:


//---------- This will connect to database1 -----------------//
$users = DB::select(...);


//---------- This will connect to database2 -----------------//
$users = DB::connection('mysql2')->select(...);

So I hope this article can solve your problem

ENJOY, HAPPY CODING!

If you are a business owner / startup looking to hire a expert Laravel developer or a expert Laravel team for your project then just drop us a line and lets take things forward.

We are providing our Laravel services across the globe. So you hire us if :

  • You are looking for Hire a Laravel developer for your project in Singapore.
  • You are looking for Hire a Laravel developer for your project in London (UK).
  • You are looking for Hire a Laravel developer for your project in Australia.
  • You are looking for Hire a Laravel developer for your project in USA, New York.
  • You are looking for Hire a Laravel developer for your project anywhere in world.

We provide option for flexible pricing so you can choose either hourly or fixed cost option.