Laravel 9/8/7 multiple database connection in this tutorial, I will demonstrate how to connect databases in Laravel 7/8/9 apps.
Laravel Multiple Database Connection Tutorial with Example
To connect multiple databases in Laravel 7/8/9 apps, follow the steps below:
- 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
Set up a database connection in the “config/database.php” directory 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 the .env file, use the following code:
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=
Set the database credentials in your project’s .env file, then update “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
In your migration, create a custom connection 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 navigate to your project’s root directory. Then, run the command below to execute our migrations for the specific database connection.
php artisan migrate --database=mysql2
If you find any errors when we run the migrate command, please let us know. We’ll provide the command below so you can clear your configuration cache.
This command resolved your problem.
php artisan config:cache
Custom Connection In Model
In your model, set the “$connection” variable. So, for that, use the following code:
<?php
class SomeModel extends Eloquent {
protected $connection = 'mysql2';
}
Custom Connection In Controller
To define the connection in your controller, use the “setConnection” method:
<?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
You have successfully created multiple database connections with and without the .env file in this article. Please leave a comment if you enjoyed the tutorial.
[…] Also Read : Laravel 9 Multiple Database Connection Tutorial […]
[…] Also Read : Laravel 9 Multiple Database Connection Tutorial […]
[…] Also Read : Laravel 9 Multiple Database Connection Tutorial […]
[…] Also Read : Laravel 9 Multiple Database Connection Tutorial […]