In my journey as a Laravel developer, I've often found the need to work with multiple database connections in my projects. Whether it's for handling diverse data sources, improving performance, or ensuring data isolation, managing multiple databases is a common requirement.
With the release of Laravel 10, the framework continues to evolve, and it's essential to stay up-to-date with the latest practices. In this article, I'll guide you through the process of setting up and using multiple database connections in Laravel 10.
We'll explore the steps required to configure these connections, create models specific to each database, run migrations and seeders, and execute queries tailored to the distinct databases.
By the end of this tutorial, you'll be well-equipped to harness the power of multiple database connections in your Laravel projects, ensuring that your application is efficient, maintainable, and ready to tackle any data-related challenges that come its way.
Let's get started on how to create multiple database connections in laravel 10, laravel 10 multiple database connections, how to use multiple databases in Laravel 9/10, and how to use multiple databases in laravel 9/10.
Open the .env
file in your Laravel project and set up your database connections. You should define multiple database connections with unique connection names, like this.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=default_database
DB_USERNAME=root
DB_PASSWORD=your_password
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=second_database
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=your_password
In this example, we've defined two database connections: DB_CONNECTION
and DB_CONNECTION_SECOND
. You can have as many connections as needed.
Now, you need to configure the database connections in Laravel's database configuration file. The configuration file is typically located at config/database.php
. In this file, you can specify the connection details and use the environment variables from your .env
file.
'connections' => [
'mysql' => [
'driver' => env('DB_CONNECTION', 'mysql'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'default_database'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
],
'mysql_second' => [
'driver' => env('DB_CONNECTION_SECOND', 'mysql'),
'host' => env('DB_HOST_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'second_database'),
'username' => env('DB_USERNAME_SECOND', 'root'),
'password' => env('DB_PASSWORD_SECOND', ''),
],
// Add more connections as needed
],
In this code, we've defined two connections: 'mysql'
and 'mysql_second'
. You can add more connections in the same way.
If you have models that need to use a specific database connection, you can specify the connection in the model itself by adding a protected $connection
property.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
protected $connection = 'mysql_second'; // Use the second database connection
...
}
When running migrations or seeders for a specific database connection, you can specify the connection using the --database
option.
For migrations:
php artisan migrate --database=mysql_second
For seeders:
php artisan db:seed --database=mysql_second
When you want to interact with a specific database connection, you can do so in your application code by specifying the connection name when using Eloquent or the query builder.
// Using Eloquent
$users = User::on('mysql_second')->get();
// Using Query Builder
$users = DB::connection('mysql_second')->table('users')->get();
That's it! You've successfully set up multiple database connections in Laravel. Remember to replace 'mysql'
and 'mysql_second'
with the actual connection names you've defined in your configuration.
You might also like:
- Read Also: How to Validate Form using Validate.js in Laravel 10
- Read Also: How to Import Excel File into Database using Python
- Read Also: Importing Excel File into Database Using Python
- Read Also: Difference between Single Quotes and Double Quotes in PHP