laravel - Laravel whereHas Filter 子关系
问题描述
是否有任何过滤员工和人力表记录的方法也不返回?
$result = Pwra::with('purchaseOrder', 'manpower')
->where('pwra_dt', $date)
->where('time_session', $session)
->whereHas('manpower.employees', function ($q) {
$q->where('status', 1);
})
->get();
Pwra 类
public function manpower()
{
return $this->hasMany('App\Models\Manpower', 'pwra_uuid', 'pwra_uuid');
}
人力班
public function employee()
{
return $this->hasOne('App\Models\Employees', 'employees_uuid', 'employees_uuid')->where('status', 1);
}
我的预期是:当员工状态 = 0 时,它不会返回任何记录,甚至人力。
解决方案
你可以试试2级whereHas()
$result = Pwra::with('purchaseOrder', 'manpower')
->where('pwra_dt', $date)
->where('time_session', $session)
->whereHas('manpower', function ($q) {
$q->whereHas('employees',function($subQ){
$subQ->where('status', 1);
});
})
->get();
whereHas('manpower.employees'
不起作用
推荐阅读
- mongodb - 在 mongoDB 上增加匹配特定要求的子文档
- java - Spring JPA Data ManyToOne 列排序与子实体属性,也允许空值
- python - 如何打印()一个命令一次
- visual-studio - EF Core Analyzer RawSqlStringInjectionDiagnosticAnalyzer 错误
- python - 使用正则表达式 python 提取文本块
- python - Python中的值替换递归
- conemu - win10使用cmder有多余空间
- javascript - 无法使用 JavaScript 获取响应状态代码
- speech-recognition - 过滤掉某些语音 Google Speech API
- visual-studio-code - 将 VS Code 光标保持在屏幕上