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

    Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...

0 Comment

You must be logged in to post a comment.