2

I have a Laravel 5.2 project with a DB master-slave setup. When running something like Model::onWriteConnection()->with('relationship')->find($id), only the find() query is run against the write connection; the with() query is still run against a slave.

This particular query must be run against the master connection, since the relevant data may not have been replicated to the slaves yet.

Is there a way to force all parts of the eloquent query to run against the write connection?

quakes
  • 431
  • 4
  • 9

1 Answers1

3

try this:

Model::onWriteConnection()->with(['relationship'=>function($query){
   $query->useWritePdo();
}])->find($id)

look at Constraining Eager Loads

quakes
  • 431
  • 4
  • 9
Amir Bar
  • 3,007
  • 2
  • 29
  • 47
  • Smart! I didn't think of trying to use eager load constraints in this manner. However, onWriteConnection() is not defined on the QueryBuilder; but useWritePdo() is, and it does seem to work! Thanks! – quakes Sep 06 '16 at 11:40