logo
podcast Podcast
get help Get Unstuck

Using RefreshDatabase with multiple connections

A simple, but undocumented, feature in Laravel

Joel Clermont
Joel Clermont
2025-03-14

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.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.