Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 9 Multiple Database Connections Example Tutorial

Laravel 9 Multiple Database Connections Example Tutorial

Posted on February 18, 2022December 11, 2022 By XpertPhp

In this tutorial, we will tell you how to connect multiple databases with the Laravel Framework (Laravel 9 multiple database connections example tutorial). so we will learn to you how to connect multiple MySQL databases with laravel in this tutorial.

The laravel can easily handle the multiple databases and so we can easily access the multiple databases in the laravel. normally we handle a single database connection with Mysql and MongoDB. but we have a large amount of data. that handle for we have to need multiple database connections.

Let’s go we follow the below steps for how to connect multiple databases with laravel application.

Overview

Step 1: Setting Database Configuration
Step 2: Use the Env variable
Step 3: Create Table using Custom connection migration
Step 4: How can use with Model and Controller
Step 5: How can use with Query Builder

Step 1: Setting Database Configuration

we will open the .env file and set the first database and second database credentials detail in the .env file. See below changes in a .env file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//for the first database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=root
 
// for the second database
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=database2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=root

Step 2: Use the Env variable

Now, we will use the constant env variable in database.php file. so see below code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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,
        ],
.....    
?>
See also  Laravel 7 Send Email Example Tutorial

Step 3: Create Table using Custom connection migration

we can easily create the table using the custom connection migration. but here we have to set the particular connection name then we can create the custom migration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
...
public function up()
{
    Schema::connection('mysql2')->create('student', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email')->unique();
        $table->timestamps();
    });
}
...
 
?>

Step 4: How can use with Model and Controller

Student.php

1
2
3
4
5
6
7
8
9
10
11
12
<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
  
class Student extends Model
{
    protected $connection = 'mysql2';
}
 
?>

StudentController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
  
class StudentController extends BaseController
{
    public function student_detail()
    {
        $student = new Student;
        $student->setConnection('mysql2');
        $student_detail = $student->find(1);
        return $student_detail;
    }
}
?>

Step 5: How can use with Query Builder

1
2
$student_detail = DB::connection('mysql2')->table("student")->get();
print_r($student_detail);

Laravel Tags:laravel 9 example, laravel 9 tutorial

Post navigation

Previous Post: Laravel 9 Socialite Github Login Tutorial Example
Next Post: Laravel 9 Instamojo Payment Gateway Integration Example Tutorial

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com