Eloquent pre-loading relationships against DB write connection

。_饼干妹妹 提交于 2019-12-12 17:13:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!