PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Written By

How to use multiple database in Laravel

How to use multiple database in Laravel

Laravel is a free, open-source PHP framework, created by Taylor Otwell. It follows a Model-View-Controller (MVC) design pattern. Laravel reuses the existing components of different frameworks which helps to create a perfect and outstanding web application. If you are familiar with PHP, Advance PHP it will make your task easier. Laravel is secure and prevents web attacks using their CSRF (Cross-site Request Forgery) protection.

While developing a web application times we need to use multiple databases because of project requirements or large-scale projects, in this time laravel allows us to use multiple database connections. You can easily learn how to use multiple databases with laravel in this blog.

Add Database Connection Detail in .env

DB_HOST_SECOND="DB HOST HERE"
DB_PORT_SECOND="DB PORT HERE"
DB_DATABASE_SECOND="DATABASE NAME HERE"
DB_USERNAME_SECOND="DB USERNAME HERE"
DB_PASSWORD_SECOND="DB PASSWORD HERE"

As you can see, you can go with multiple if you want to use more databases, also you can go with other database engines. Here we use MySQL.

Configure database detail in config / database.php

add detail in the connections array.

<?php

return [
   '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', ''),
     ],
     'mysql_second'=>[
       'driver' => 'mysql',
       'url' => env('DATABASE_URL'),
       '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', ''),
     ]
   ]
];

?>

Note: Add All Details Of Database Connection Here we add only database detail for learning purposes.

Now We learn How to use this connection with Schema , Query , and Eloquent Model .

Schema Builder

With Schema Builder, You can use any connection by simply run the connection() method:

Schema::connection('mysql_second')->create('table_name', function($table){
   // entire code here
});

Query Builder

Similar to Schema Builder, you can define a connection in Query Builder:

$posts = \DB::connection('mysql_second')->select('id','title')->get();

Eloquent Model

Similar to Query Builder, you can define a connection in the Eloquent Model:

<?php

namespace App\Models;

class Post extends Eloquent {
   protected $connection = 'mysql_second';
}

?>

Also, you can use the connection in custom join queries. with this simple example:

\DB::table('posts')->join('mysql_second.types as secondDbType','posts.code','=','secondDbType.code')->first();

Comments (0)

loading comments