Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram
Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram Pinterest LinkedIn Reddit
Readytocode
You are at:Home How to Connect Laravel 8 with Multiple Database

Laravel

How to Connect Laravel 8 with Multiple Database

JustinBy JustinApril 20, 2022Updated:June 4, 2022No Comments3 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email




Hello Laravel Lovers, Today i will show you how we can connect laravel to multiple database .

Here i will show you with Mysql , You can also use which one DB you will love.

We will do this task in steps.

  1. Set .env files For Multiple Database
  2. Some changes in config/database.php file
  3. Multiple Database Connection with Model
  4. Multiple Database Connection with Query Builder

Step 1. Setup .env files For Multiple Database

set your .env file like the below code.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db_1
DB_USERNAME=root
DB_PASSWORD=

// Database 2

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=laravel_db_2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=

 

Step 2. Some changes in config/database.php file

Open the database.php file and add new connection like below code

<?php

use Illuminate\Support\Str;

return [

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

    'connections' => [

        .....   

        'mysql' => [

            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'mysql2' => [
            'driver' => env('DB_CONNECTION_SECOND'),
            'host' => env('DB_HOST_SECOND', '127.0.0.1'),
            'port' => env('DB_PORT_SECOND', '3306'),
            'database' => env('DB_DATABASE_SECOND', 'forge'),
            'username' => env('DB_USERNAME_SECOND', 'forge'),
            'password' => env('DB_PASSWORD_SECOND', ''),
            'unix_socket' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ],

.....   
.....
.....
?>

 

Step 3. Multiple Database Connection with Model

Create a model and set like below code for the data accessing from second database and if you want to get data from default database then not use ” protected $connection = ‘mysql2’ “.

<?php

namespace App;
  
use Illuminate\Database\Eloquent\Model;
   
class TestModel extends Model
{
    protected $connection = 'mysql2';
    ...
}

 

Step 4. Multiple Database Connection with Query Builder

Here in first function we will get data from default database. This code will be general as we already writing .

class UsersController extends Controller
{

    public function getUser(){
          $users = DB::table("users")->get();
          print_r($users);
    }
}

In below method will fetch data from second database.

class UsersController extends Controller { 
           .....
           .....
           public function getUserfrom2DB(){
                  $users = DB::connection('mysql2')->table("users")->get();
                  print_r($users); 
           } 
}

 

Conclusion

In this tutorial we see how to connect with multiple database. Hope this post will help you to connect with multiple database.

Thanks for reading this post.

 

 

 

 

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0

Like this:

Like Loading...

Related

connect multiple database in laravel Laravel 9 multiple database
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Justin
  • Website

Related Posts

How to Generate image from text with Laravel 10

May 16, 2023

Laravel 10 Restrict User Access

May 3, 2023

Laravel 10 Pagination Example

May 3, 2023

Leave A Reply Cancel Reply

Featured Posts
  • Why meta tag is important for seo 1
    How to change react Page title, Page Description and some other meta Tag
    • August 4, 2023
  • 2
    How to Generate image from text with Laravel 10
    • May 16, 2023
  • Laravel 10 Restrict User Access 3
    Laravel 10 Restrict User Access
    • May 3, 2023
  • Laravel 10 Pagination Example 4
    Laravel 10 Pagination Example
    • May 3, 2023
  • install Steam on Ubuntu 5
    How to install Steam on Ubuntu 22.04 step by step
    • April 20, 2023



Readytocode
Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Privacy Policy
© 2025 ReadyToCode.

Type above and press Enter to search. Press Esc to cancel.

%d