1 | initial version |
The process for establishing connections to multiple databases using Laravel Sail involves the following steps:
Create multiple database containers in Docker Compose file by specifying the required environment variables for each database service.
Configure the config/database.php
file in your Laravel application with the new connections for each database.
Use the DB_CONNECTION
environment variable to specify which connection to use for a given query. You can switch connections by updating the environment variable before running the query.
To migrate multiple databases, use the sail art migrate --database=CONNECTION_NAME
command. You can replace CONNECTION_NAME
with the actual name of the connection you want to migrate.
You can also run commands on a specific connection by prefixing the command with the CONNECTION_NAME
parameter, for example sail exec mysql2 mysqldump ...
.
When performing migrations or running queries on a different connection than the default, you should specify the other connection's name either using the $onConnection
property in your migration files, or the on()
method when querying.
By following the above steps, you can easily establish multiple database connections in Laravel Sail.