In Laravel, you can configure multiple databases by defining multiple database connections in your
config/database.php file. Each connection can have its own configuration settings such as database name, username, password, host, and so on.
Here’s an example of how to configure multiple databases in Laravel:
- Open the
config/database.phpfile and locate the
- Add a new connection to the
connectionsarray, using the following template:
'connection_name' => [ 'driver' => 'mysql', 'host' => env('DB_HOST_connection_name', 'localhost'), 'database' => env('DB_DATABASE_connection_name', 'database_name'), 'username' => env('DB_USERNAME_connection_name', 'username'), 'password' => env('DB_PASSWORD_connection_name', 'password'), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ],
connection_name with the name of your connection, and replace the values of
DB_PASSWORD_connection_name with the appropriate values for your database.In your model, you can specify which connection to use by adding the following line to your model class:
protected $connection = 'connection_name';
connection_name with the name of your connection.When performing database operations, you can use the desired connection by specifying the name of the connection in your query:
connection_name with the name of your connection.
With these steps, you have successfully configured multiple databases in Laravel and can now work with them as needed.