Connect Multiple databases in the same Laravel project



Sometimes we need two databases for the project, we can connect our Laravel Application with external data from another database – for example, where the blog is stored, managed by 3rd party software. How to handle it in Laravel 5?

Connect Multiple databases in the same Laravel project

Step: 1 Update config/database.php

In config/database.php where we can see an array of all connections like MySQL, PostgreSQL, SQL Server here is we can add more than one connection with the same database system, In this Post, we will be MySQL. So, let’s copy a default array item and paste as another one underneath – and call it mysql_external:

 'connections' => [

        'sqlite' => [
        	// ...
        ],

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'mysql_external' => [
            'driver'    => 'mysql',
            'host'      => env('DB_EXT_HOST', 'localhost'),
            'database'  => env('DB_EXT_DATABASE', 'forge'),
            'username'  => env('DB_EXT_USERNAME', 'forge'),
            'password'  => env('DB_EXT_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'pgsql' => [
        	// ...
        ],

        'sqlsrv' => [
        	// ...
        ],

    ],

 

Step: 2 Update .env file

Now In the second step, we have two options: provide those external credentials here in the same config/database.php file, or add them into the .env file.

DB_HOST=localhost
DB_DATABASE=testing
DB_USERNAME=homestead
DB_PASSWORD=secret

DB_EXT_HOST=localhost
DB_EXT_DATABASE=testing2
DB_EXT_USERNAME=homestead
DB_EXT_PASSWORD=secret

 

Step: 3 Connect to the second database

So, we have a new connection named mysql_external which has the credentials provided, now time to use it. So in your code.

where you want to use or access data from that external database, you just call DB::connection() method and assign the result to the variable. Then we can use that variable to make any queries.

Here is an Example.

class TestController extends Controller
{

    public function getTest()
    {
        $db_ext = \DB::connection('mysql_external');
        $countries = $db_ext->table('countries')->get();
        print_r($countries);
    }

}

So here we completed this post Connect Multiple databases in the same Laravel project.


About Author
  • user profile
    Brijpal Sharma

    brijpalsharma99@gmail.com, January 29th, 2018

    Hi, I am a web developer, I working on Laravel and CodeIgniter framework, I love to share knowledge and tutorials so I here...


0 Comment


You must be logged in to post a comment.


You may also be interested in...
jQuery plugin to validate the maximum length of the field and count characters.
Deploy Laravel Application To Shared Hosting The Easy and save Way
Laravel advance search filters with optional fields.
Laravel Migrations: Check if Table/Column Already Exists
Connect Multiple databases in the same Laravel project
SEO tool for Laravel Application
Paypal payment gateway integration in Laravel 5.7 step by step
How to Check User online or not in Laravel
How to keep filter data after the click to next button in Laravel pagination
Which is better for web development, Laravel or Flask?