Hello Laravel Lovers, Today i will show you how we can connect laravel to multiple database .
Here i will show you with Mysql , You can also use which one DB you will love.
We will do this task in steps.
- Set .env files For Multiple Database
- Some changes in config/database.php file
- Multiple Database Connection with Model
- Multiple Database Connection with Query Builder
Step 1. Setup .env files For Multiple Database
set your .env file like the below code.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_db_1 DB_USERNAME=root DB_PASSWORD= // Database 2 DB_CONNECTION_SECOND=mysql DB_HOST_SECOND=127.0.0.1 DB_PORT_SECOND=3306 DB_DATABASE_SECOND=laravel_db_2 DB_USERNAME_SECOND=root DB_PASSWORD_SECOND=
Step 2. Some changes in config/database.php file
Open the database.php file and add new connection like below code
<?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. Multiple Database Connection with Model
Create a model and set like below code for the data accessing from second database and if you want to get data from default database then not use ” protected $connection = ‘mysql2’ “.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class TestModel extends Model { protected $connection = 'mysql2'; ... }
Step 4. Multiple Database Connection with Query Builder
Here in first function we will get data from default database. This code will be general as we already writing .
class UsersController extends Controller { public function getUser(){ $users = DB::table("users")->get(); print_r($users); } }
In below method will fetch data from second database.
class UsersController extends Controller { ..... ..... public function getUserfrom2DB(){ $users = DB::connection('mysql2')->table("users")->get(); print_r($users); } }
Conclusion
In this tutorial we see how to connect with multiple database. Hope this post will help you to connect with multiple database.
Thanks for reading this post.