php - 有条件的,基于另一个模型的价值
问题描述
我有三个模型
发票
工作室
R_studio_pay
关系如下
工作室有很多发票
工作室有一个R_studio_pay
如果 R_studio_pay 中的列 is_r 为 1,我需要获取发票->where('recurring', 'single')->get() 否则我不想要 where 子句。
我尝试使用 whereHas studio-> wherehas R_studio_pay 但有条件的 where 无法完成。
$invoices = invoice::with('studio')->whereHas('studio', function($query) {
$query->whereHas('r_studio_pay', function($query) {
$query->where('is_r', 1);
});
})->where('recurring', 'single')
但不能在 where 条件下应用。
解决方案
你在正确的轨道上。但是,您应该在回调中添加一个 return 语句。return $query->where('is_r', 1);
而不是$query->where('is_r', 1);
. 试试下面的代码。
$invoices = invoice::with('studio')->whereHas('studio', function($query){
return $query->whereHas('r_studio_pay', function($query){
return $query->where('is_r', 1);
});
})->where('recurring', 'single');
推荐阅读
- php - 如何将数组添加到模型中的另一个数组中?
- java - Java 从 Python 服务器接收消息 - 套接字
- handler - Mybatis - 通过存储过程从 Oracle 中提取用户定义的类型
- java - 如何使用 @InheritInverseConfiguration 和 unmappedTargetPolicy = ReportingPolicy.ERROR 忽略未映射的目标属性
- r - R中有没有办法在MySQL的数据框中散列密码?
- python - 命令引发异常:TypeError: send() got an unexpected keyword for embed
- c# - 如何将数据行转换为通用模型类?
- nestjs - 如何在模块之间使用公共服务?
- javascript - 在 NestJs 和 ReactJS 中使用 websocket
- json - Azure Api 服务上的 JSON 反序列化失败