Laravel allows connecting multiple databases with different database engines. Laravel has inbuilt support for multiple database systems.
While developing a web application sometimes we need to use multiple databases because of project requirement or large scale projects, in this time Laravel allows using multiple database connections. In this blog, we'll explore how to use multiple databases in Laravel.
Configuring the Second Database Connection
To configure a second database connection in Laravel, you'll need to update the config/database.php
file. In this example, we'll create a second connection named users
. Here's an example configuration for the users
connection:
<?php
'connections' => [
'mysql' => [
// ...
],
'users' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_USERS', '127.0.0.1'),
'port' => env('DB_PORT_USERS', '3306'),
'database' => env('DB_DATABASE_USERS', 'users'),
'username' => env('DB_USERNAME_USERS', 'root'),
'password' => env('DB_PASSWORD_USERS', ''),
'unix_socket' => env('DB_SOCKET_USERS', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
],
In this configuration, we're specifying a new connection named users
, which uses the same MySQL driver as the default connection. We're also setting the database connection details for the users
connection.
Note that we've used the env()
function to retrieve values from the .env
file. By default, Laravel expects a .env
file in the root directory of your project, which contains environment-specific configuration details. Here's an example of how to configure the users
connection in the .env
file:
DB_CONNECTION_USERS=mysql
DB_HOST_USERS=127.0.0.1
DB_PORT_USERS=3306
DB_DATABASE_USERS=users
DB_USERNAME_USERS=root
DB_PASSWORD_USERS=
In this example, we're setting the connection details for the users
database connection. Make sure to replace the values for DB_HOST_USERS
, DB_PORT_USERS
, DB_DATABASE_USERS
, DB_USERNAME_USERS
, and DB_PASSWORD_USERS
with the appropriate values for your users
database connection.
Using the Second Database Connection
Now that we've configured a second database connection, we can use it in our application. To use the users
connection in a query, you can specify the connection name using the connection()
method:
In this example, we're using the users
connection to retrieve all users from the users
table. Note that we're using the connection()
method to specify the users
connection for this query.
$users = DB::connection('users')->table('users')->get();
In this example, we're using the connection() method to specify the users connection when querying the users table.
You can also specify the connection name in your model by setting the $connection
property:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The connection name for the model.
*
* @var string
*/
protected $connection = 'users';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
}
In this example, we've set the $connection
property of the User
model to users
. This tells Laravel to use the users
connection when querying the users
table for that model.
Conclusion
In this blog post, we've explored how to use multiple databases in Laravel. By configuring a second database connection and specifying the connection name in your queries or models, you can easily work with multiple databases in your Laravel application.