Changing the database connection on Laravel's Query Builder

While working for a client whose codebase had a lot of legacy code I faced an interesting challenge, I needed to change the database connection on an already instantiated query builder, and I couldn't do it the Laravel way.

Our client introduced the Master Slave Replication into an existing code base that was fairly complex. Since the service which was altered was written with the Method Chaining pattern, by the time we were at the method that would actually fetch from the database a lot of things would already be set upstream, so instead of a Model or a Connection instance I would get an Eloquent or Query Builder instance.

The first thing that came to my mind was to set the connection in the constructor of the service depending on some conditions, but in the end that actually caused a bug where we would try to do write statements on the slave database. So, that was a no go.

I couldn't go with the approach that Laravel offers out of the box, a read and write config, because that would force us to always read from the slave database, and we wanted to be able to manually control when to read from which database. But wait?! Doesn't Laravel provide a method for that?! Well, yes it does, and it's called useWritePdo(), but in our case, once again, we couldn't go the Laravel way because it would mean a lot of query building methods would need to have that added, and that's a boring and tedious work.

Rewriting the service was out of the question because it would affect its public API and it was used all over the codebase, not to say that it was huge, so I had to get creative.

I know some people hate traits but I love them, they provide us with so much flexibility, so I created one and named it Slavable, almost went with Enslavable, but that could come across as a bit racist nowadays. * ba dum ts * Now that the awkward silence has passed after this bad joke, let's take a look at the code.

Worked like a charm! What it does is it takes the old query instance, takes all its SQL string components and bindings, and finally transfers it to a new query with a new connection. Pretty nifty isn't it?