Now, let's see an example of laravel 9 multiple database connections. I explained simply how to use multiple database connections in laravel 9. if you want to see an example of multiple database connections in laravel 9 then you are in the right place. you can understand the concept of laravel 9 multiple database connections .env. you will do the following things for multiple DB connections in laravel 9.
I will give you step by step implementation of how to use laravel 9 multiple DB connections using the .env file. we will add a configuration variable on the .env file and use it in the database configuration file. You can just follow me, I will also learn how to work with migration, model and database queries for multiple database connections.
As we know sometimes we need to use multiple database connections like MySQL, MongoDB, etc. I can say when you work with a large number of projects then you will need maybe. So let's follow bellow steps.
Set ENV Variable:
Here, you need to set configuration variable on .env file. let's create as bellow:
Now, as we created variable in env file, we need to use that variable on config file so let's open database.php file and add new connections key as like bellow:
I will give write two routes with getting products from different database connections. you can see simple example with DB.
let's add two routes in your file:
routes/web.php
<?php use Illuminate\Support\Facades\Route; /*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/ /*--------------------------------------------------------------------------------------Getting Records of Mysql Database Connections----------------------------------------------------------------------------------------*/Route::get('/get-mysql-products', function () { $products = DB::table("products")->get(); dd($products);}); /*--------------------------------------------------------------------------------------Getting Records of Mysql Second Database Connections----------------------------------------------------------------------------------------*/Route::get('/get-mysql-second-products', function () { $products = DB::connection('mysql_second')->table("products")->get(); dd($products);});
Multiple Database Connections with Migration:
you can create separate migrations for multiple database connections:
Default:
<?php.....public function up(){ Schema::create('blog', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('body')->nullable(); $table->timestamps(); });}.....
Second Database:
<?php.....public function up(){ Schema::connection('mysql_second')->create('blog', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('body')->nullable(); $table->timestamps(); });}.....
Multiple Database Connections with Model:
Default:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model; class Product extends Model{ use HasFactory; protected $fillable = [ 'name', 'detail' ];}
Second Database:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model; class Product extends Model{ use HasFactory; protected $connection = 'mysql_second'; protected $fillable = [ 'name', 'detail' ];}
Multiple Database Connections in Controller:
Default:
<?php use App\Models\Product; class ProductController extends BaseController{ /** * Write code on Method * * @return response() */ public function getRecord() { $products = Product::get(); return $products; }}