How to use multiple databases in Laravel
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Laravel supports more than one database connection out of the box, which is useful for reporting databases, tenant-specific databases, legacy systems, or read and write separation. The important part is not only defining the extra connections, but also making sure each query, model, migration, and transaction is pointed at the correct one.
Define Multiple Connections
Laravel stores connection definitions in config/database.php. You can add as many named connections as your application needs.
Then define the matching values in .env:
Laravel’s DB::connection('name') API uses these names directly, so clear naming helps a lot.
Run Queries on a Specific Connection
For one-off queries, call connection() on the DB facade:
This keeps the default connection unchanged while routing just that query to the reporting database.
If you need the raw PDO connection, Laravel also exposes it:
That can help when integrating with a library that expects a PDO handle, though it is usually better to stay inside Laravel’s query builder or Eloquent when possible.
Bind an Eloquent Model to Another Database
If a model always belongs to a non-default database, set the $connection property on the model.
After that, Eloquent queries for AuditLog automatically use the reporting connection:
This is cleaner than repeating DB::connection('reporting') throughout the codebase.
Migrations and Schema Operations
When a schema change belongs to a specific database, scope the migration or schema call explicitly.
From the command line:
Inside application code:
Be deliberate here. It is easy to run a migration on the default database by accident and not notice until much later.
Dynamic Connections for Tenant-Like Cases
Some applications build a connection at runtime. A common example is a per-tenant database. Laravel lets you inject connection settings dynamically:
That pattern is powerful, but it raises the bar for testing, error handling, and connection lifecycle management.
Common Pitfalls
The most common mistake is assuming that switching connections for one query also switches it for related models. It does not. A model without a $connection property still uses the default connection.
Another issue is cached configuration. If you change .env or config/database.php and Laravel still seems to use old values, clear and rebuild config cache with commands such as php artisan config:clear or php artisan config:cache.
Transactions are another trap. A transaction started on one connection does not automatically include operations on another connection. If you write to two databases in one request, you need to handle consistency deliberately because Laravel is not giving you a distributed transaction.
Finally, do not mix unrelated concerns in one connection name. Names such as mysql2 work, but names like reporting, tenant, or legacy communicate intent much better.
Summary
- Define each database connection in
config/database.phpand back it with environment variables. - Use
DB::connection('name')for query-level routing. - Set a model’s
$connectionproperty when it always belongs to a non-default database. - Scope migrations and schema changes to the correct database explicitly.
- Be careful with config caching, cross-database consistency, and runtime-generated connections.
Related reading
- How to use MySQL DECIMAL?
- How to use mysql JOIN without ON condition?
- How to use MySQLdb with Python and Django in OSX 10.6?
- How to use MySQLdb with Python and Django in OSX 10.6?
- how to use pymysql executemany insert many rows and get the ids of each rows
- How to use Spring Boot with MySQL database and JPA?
- How to use Spring managed Hibernate interceptors in Spring Boot?
- How to use the dumped data by mongodump?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.