Laravel 10/8/7 multiple database connection; through this tutorial, i am going to show you how to connect databases in laravel 7/8/9 apps.
Laravel Multiple Database Connection Example
Follow the following steps to connect multiple database in laravel 7/8/9 apps; is as follows:
- 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
Visit to “config / database.php” directory and set up database connection; is as follows:
<?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
To update .env file using the below code; is as follows:
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=
To set the database credential in your project .env file and then update your “config/database.php” with the following code:
<?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
Create custom connection in your migration; is as follows:
<?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 go to your project root directory, After that run the below command for executing our migrations for the specific database connection.
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;
php artisan config:cache
Custom Connection In Model
To set the “$connection” variable in your model. So use the below code for that:
<?php class SomeModel extends Eloquent { protected $connection = 'mysql2'; }
Custom Connection In Controller
To define the connection in your controller using the “setConnection” method; is as follows:
<?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.
DB::connection('mysql2')->select(...)->(...);
Conclusion
In this article, you have successfully created multiple database connections with the .env file and without using the .env file.