问题
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?
回答1:
try this:
Model::onWriteConnection()->with(['relationship'=>function($query){
$query->useWritePdo();
}])->find($id)
look at Constraining Eager Loads
来源:https://stackoverflow.com/questions/39347718/eloquent-pre-loading-relationships-against-db-write-connection