How to use multiple databases in Laravel

Today, We want to share with you How to use multiple databases in Laravel.In this post we will show you Laravel Multiple Database Connections, hear for How to Run Laravel Using Multiple Database Connections we will give you demo and example for implement.In this post, we will learn about How to Use Multiple Database Connections in Laravel Application with an example.

How to use multiple databases in Laravel

There are the Following The simple About How to use multiple databases in Laravel Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel 5.8 connect to multiple databases, so the laravel using multiple database connections for this example is following below.

Using .env >= 5.0 (tested on 5.6)

In .env file settings

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pakainfo
DB_USERNAME=modi_information
[email protected]

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=atmiya
DB_USERNAME_SECOND=modi_information
[email protected]

step 2: In config/database.php

'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'),
],

'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'),
],

IMP Note:In Laravel mysql2 if DB_username as well as DB_password is same, after that We can use simple env(‘DB_USERNAME’) which is here metioned in .env first few some lines.

Laravel Multiple db config Without .env <5.0

app/config/database.php

return array(

    'default' => 'mysql',

    'connections' => array(

        # Primary/Default database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'pakainfo',
            'username'  => 'modi_information',
            'password'  => '[email protected]'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Laravel Secondary database connection
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'atmiya',
            'username'  => 'modi_information',
            'password'  => '[email protected]'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

Schema

Schema::connection('mysql2')->create('products', function($table)
{
    $table->increments('id'):
});

Laravel Query Builder

$users = DB::connection('mysql2')->select(...);

using Laravel Eloquent

class Products extends Eloquent {

    protected $connection = 'mysql2';

}

setConnection in Laravel

class SomeController extends BaseController {

    public function someMethod()
    {
        $products = new Products;

        $products->setConnection('mysql2'); // laravel non-static method

        $product_data = $products->find(1);

        $product_data = Products::on('mysql2')->find(1); // laravel static method

        return $product_data;
    }

}

Web Programming Tutorials Example with Demo

Read :

Also Read This ๐Ÿ‘‰   PHP Laravel 6 Multiple Database Connections Tutorial

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to use multiple databases in Laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.