Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process for establishing connections to multiple databases using Laravel Sail involves the following steps:

  1. Create multiple database containers in Docker Compose file by specifying the required environment variables for each database service.

  2. Configure the config/database.php file in your Laravel application with the new connections for each database.

  3. 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.

  4. 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.

  5. You can also run commands on a specific connection by prefixing the command with the CONNECTION_NAME parameter, for example sail exec mysql2 mysqldump ....

  6. 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.