In this tutorial you will learn about the Laravel 7/6 Multiple Database Connections In one application and its application with practical example.
In this Laravel 7/6 Multiple Database Connections In one application tutorial I will show you how to connect or use multiple database in single laravel project. In this tutorial you will learn to connect or use multiple database in single laravel project.
In this tutorial, we would love to share with you how to connect single or multiple databases using a laravel application. Sometimes, we need to connect multiple databases to the Laravel application.
Laravel 7/6 Multiple Database Connections In one application
- Without .env Database Connection
- With .env Database Connection
- Custom Connection In Migration
- Custom Connection In Model
- Custom Connection In Controller
- Custom Connection with Query Builder
- Conclusion
Without .env Database Connection
In Laravel, there is a database configuration file is located on “config/database.php”. From this file you can set up more than one database connection. If your app uses data from two different MySql databases, you can easily define them.
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 |
<?php return => [ 'connections' => [ // Default database connection 'mysql' => [ 'driver' => 'mysql', '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' => '', 'strict' => true, 'engine' => null, ], // Custom new database connection 'mysql2' => [ 'driver' => 'mysql', 'host' => 'localhost', 'port' => '3306', 'database' => 'lara_multiple', 'username' => 'root', 'password' => '', 'unix_socket' => '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], ] ] |
With .env Database Connection
1 2 3 4 5 6 |
DB_CONNECTION_SECOND=mysql DB_HOST_SECOND=127.0.0.1 DB_PORT_SECOND=3306 DB_DATABASE_SECOND=multi_lara DB_USERNAME_SECOND=root DB_PASSWORD_SECOND= |
Next, you can set the database credential in your project .env file and then update your “config/database.php” with the following code given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php return => [ 'connections' => [ // Custom database connection 'mysql2' => [ 'driver' => env('DB_CONNECTION_SECOND'), 'host' => env('DB_HOST_SECOND'), 'port' => env('DB_PORT_SECOND'), 'database' => env('DB_DATABASE_SECOND'), 'username' => env('DB_USERNAME_SECOND'), 'password' => env('DB_PASSWORD_SECOND'), ... ], ] ] |
Custom Connection in Migration
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php ... public function up() { Schema::connection('mysql2')->create('multipost', function (Blueprint $table) { $table->increments('id'); $table->string('post_title'); $table->string('post_content'); $table->timestamps(); }); } ... |
Open your terminal and switch to your project directory, Now, run the following command to run our migrations for the specific database connection.
1 |
php artisan migrate --database=mysql2 |
If you found any error when we execute the migrate command. So that time you can clear your config cache, we will provide the command below:
This command fixed your issue;
1 |
php artisan config:cache |
Custom Connection In Model
You can set the “$connection” variable in your model. So use the below code for that:
1 2 3 4 |
<?php class SomeModel extends Eloquent { protected $connection = 'mysql2'; } |
Custom Connection In Controller
You can also define the connection in your controller using the “setConnection” method.
1 2 3 4 5 6 7 8 9 10 11 |
<?php class xyzController extends BaseController { public function someMethod() { $someModel = new SomeModel; $someModel->setConnection('mysql2'); $something = $someModel->find(1); return $something; } } |
Custom Connection with Query Builder
You can also define a connection on the query builder.
1 |
DB::connection('mysql2')->select(...)->(...); |