Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

Using RefreshDatabase with multiple connections

A simple, but undocumented, feature in Laravel

Joel Clermont
Joel Clermont
2025-03-14

From our daily Laravel tips email. Get the next one in your inbox.

Laravel offers a ton of functionality, and every single option is not described in the documentation.

But hey, this is open source, so we can look at the code any time we want and see how things work.

I was working on a project recently that used multiple database connections, and only the default connection was getting refreshed when running tests.

Looking at the RefreshDatabase trait, I saw that it checks for the presence of a $connectionsToTransact property and uses that to determine which connections to refresh. If not present, it falls back to the default connection.

So if I want to refresh multiple connections, it's a simple addition:

// tests/TestCase.php
abstract class TestCase extends BaseTestCase
{
    use RefreshDatabase;

    protected $connectionsToTransact = [
        'connection1',
        'connection2',
    ];
}

Here to help,

Joel

P.S. If you're a solo dev and don't have someone to do code review, I'd be happy to take a look. It's a really valuable process.

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.