Skip to content
  • 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

Home » Laravel » Laravel 7 Multiple Database Connections Example Tutorial

Laravel 7 Multiple Database Connections Example Tutorial

Laravel 7 Multiple Database Connections Example Tutorial

Posted on March 27, 2020February 14, 2021 By XpertPhp No Comments on Laravel 7 Multiple Database Connections Example Tutorial

In this tutorial, we will tell you how to connect multiple databases with the Laravel Framework (Laravel 7 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,
        ],
.....    
?>

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);

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Laravel 7 ajax pagination example using jquery
  • Laravel 7 Socialite Twitter Login Tutorial Example
  • Laravel 6 Pagination Example Tutorial
  • Laravel 8 Vue JS Owl Carousel Slider Example Tutorial
  • Laravel 7 integrate ckeditor with example
Laravel Tags:laravel 7, laravel 7 multiple database, multiple database connections

Post navigation

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

Latest Posts

  • Laravel Check If Foreach is Empty Example
  • Laravel Check Array Empty in Blade
  • PHP Datatables CRUD Example
  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example

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
  • 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 tricks 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 Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com