laravel 6 multiple database connections example tutorial – onlinecode
In this post we will give you information about laravel 6 multiple database connections example tutorial – onlinecode. Hear we will give you detail about laravel 6 multiple database connections example tutorial – onlinecodeAnd how to use it also give you demo for it if it is necessary.
In this tutorial, we will tell you how to connect multiple databases with the Laravel Framework (Laravel 6 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 ConfigurationStep 2: Use the Env variableStep 3: Create Table using Custom connection migrationStep 4: How can use with Model and ControllerStep 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.
//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.
<?php use IlluminateSupportStr; 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.
<?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
<?php namespace App; use IlluminateDatabaseEloquentModel; class Student extends Model { protected $connection = 'mysql2'; } ?>
StudentController.php
<?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
$student_detail = DB::connection('mysql2')->table("student")->get(); print_r($student_detail);
Hope this code and post will helped you for implement laravel 6 multiple database connections example tutorial – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs