laravel - 我将如何在 laravel eloquent 的急切加载中加入条件
问题描述
enter code here
$wo=Warehouse_other_receive_detail::with('item_info','item_info.product_sub_group')
->where('item_info.product_sub_group->sub_group_id', 2)
->get();
$wo = Warehouse_other_receive_detail::with(['item_info'=>function( $q){
$q->getQuery()->has('product_sub_group')->where('sub_group_id', 2);
}])
->get();
//但是第二个代码不是内部连接我想第一个解决//我将如何访问主模型中的函数模型特定列----解决这个->>
解决方案
使用 whereHas 而不是 where。您可以使用 whereHas 方法完成您想要的操作,该方法将对关系执行查询:
$wo = Warehouse_other_receive_detail::with('item_info')->whereHas('item_info', function ($query){
$query->where('sub_group_id',2);
})->get();
推荐阅读
- html - 如何在不使用 @import 的情况下将全局样式应用于 html、带有 SCSS 的正文
- javascript - 反应汉堡菜单不起作用
- reactjs - Typescript React map API 对具有额外属性的内部类型的响应
- python - tkinter 键盘快捷键问题
- oracle - 如何在 Oracle PLSQL 和 SQL 中执行字符串插值。给定一个带有命名占位符的文本模板,用变量替换那些
- python - 查找具有不同 numpy 大小的两个图的交集
- c - 在 C 中没有赋值的 #define 分配给什么?
- kubernetes - 无法使用来自浏览器的入口访问我在 k8s 集群上的 grafana 仪表板
- python - pyperclip.copy(str(line)) 没有正确复制
- python - 全局变量不起作用或类似的东西...... discord.py