Laravel Multiple Database Connections Step By Step

Today, We want to share with you Laravel Multiple Database Connections Step By Step.In this post we will show you laravel using multiple database connections, hear for Configuring Multiple Database Connections In Lumen we will give you demo and example for implement.In this post, we will learn about Multiple database connections in the same Laravel project with an example.

Laravel Multiple Database Connections Step By Step

There are the Following The simple About Laravel Multiple Database Connections Step By Step Full Information With Example and source code.

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

Step 1: Multiple Database Connections

Multiple database connections in the same Laravel project

.env file

//first database Configuring
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=YOUR_FIRST_DATABASE
DB_USERNAME=root
DB_PASSWORD=root

//second(External) database Configuring
DB_EXT_HOST=localhost
DB_EXT_DATABASE=YOUR_SECOND_DATABASE
DB_EXT_USERNAME=root
DB_EXT_PASSWORD=root

Database Configuring

my_app/config/database.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_OBJ,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'mysql'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'YOUR_FIRST_DATABASE'),
            'username' => env('DB_USERNAME', 'YOUR_USER_NAME'),
            'password' => env('DB_PASSWORD', 'YOUR_PASSWORD'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
		/* Start second(External) database Configuring */
		'mysql_external' => [
            'driver'    => 'mysql',
            'host'      => env('DB_EXT_HOST', 'localhost'),
            'database'  => env('DB_EXT_DATABASE', 'YOUR_SECOND_DATABASE'),
            'username'  => env('DB_EXT_USERNAME', 'YOUR_USER_NAME'),
            'password'  => env('DB_EXT_PASSWORD', 'YOUR_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
		/* End second(External) database Configuring*/
        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('LIVE_REDIS_PASSWORD', null),
            'port' => env('LIVE_REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

Step 2: Define a laravel Route

web.php

Route::get('/check-external-database', '[email protected]');

Step 3 Create a Laravel Controller

TestController.php

<?php
namespace App\Http\Controllers;
use Auth;
use App;
class TestController extends Controller
{

    public function getTest()
    {
        $db_ext = \DB::connection('mysql_external');
        $countries = $db_ext->table('countries')->get();
		echo "<pre>";
        print_r($countries);
		echo "</pre>";
		
		$products = $db_ext->table('products')->get();
		echo "<pre>";
        print_r($products);
		echo "</pre>";
    }

}

Web Programming Tutorials Example with Demo

Read :

Summary

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

I hope you get an idea about Laravel Multiple Database Connections Step By Step.
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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Laravel 6 Multiple Database Connections Tutorial
  2.   Simple Model Popup Box using HTML CSS and JavaScript
  3.   install and/or enable a php accelerator (highly recommended)
  4.   Laravel Advance Database Query Builder
  5.   PHP Laravel 6 Flash Message Notification
  6.   Laravel updateOrCreate, firstOrNew, firstOr and firstOrCreate Methods
  7.   Laravel 6 try catch Database Transaction
  8.   Download Sample woocommerce products Data in CSV
  9.   How to use multiple databases in Laravel
  10.   Multiple Database Connections using Laravel