Dwight Watson's blog

Multiple database read/write connections in Laravel

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

This post follows up on my previous post Multiple read-only database slaves in Laravel after I got a helpful comment from Ezequiel Russo who showed me an even better way to use multiple database read/write connections in Laravel - one that works with Laravel's config caching.

It turns out that Laravel will automatically pick a database connection at random if provided with an array, so there's no need to pick a random connection yourself. This makes it really easy to define your connections and have it work with php artisan config:cache.

'mysql' => [
'driver' => 'mysql',
'read' => [
'host' => ['193.168.1.1', '194.168.1.1']
],
'write' => [
'host' => '196.168.1.2'
],
//
]

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.