针对数据库写连接的雄辩的预加载关系 [英] Eloquent pre-loading relationships against DB write connection

查看:67
本文介绍了针对数据库写连接的雄辩的预加载关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数据库主从设置的Laravel 5.2项目.当运行诸如Model::onWriteConnection()->with('relationship')->find($id)之类的内容时,仅针对写入连接运行find()查询; with()查询仍然针对从属服务器运行.

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?

推荐答案

尝试一下:

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

查看约束渴望的负载

这篇关于针对数据库写连接的雄辩的预加载关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆