【问题标题】:Laravel whereHas Filter sub relationLaravel whereHas Filter 子关系
【发布时间】:2020-10-15 17:26:04
【问题描述】:

是否有任何过滤员工和人力表记录的方法也不返回?

$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);
}

我的预期是:当员工Status = 0时,它不会返回任何记录,甚至人力。

【问题讨论】:

  • 我已经尝试过,但仍然不适合我
  • 我认为你需要做 2 次 whereHasjoin
  • 嗯...任何例子 2 次 whereHas ?
  • 它不起作用with('manpower.employees')

标签: laravel


【解决方案1】:

你可以试试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' 不行

【讨论】:

  • 我已经尝试过这个查询,但它一直显示状态 = 0 的员工
  • status varchar 或 int 的数据类型是什么?
  • INTstatus
  • 如果使用manpowerwherehas() 方法。然后你不需要用with() 方法来做,with('purchaseOrder', 'manpower') 将是with('purchaseOrder') 因为你用wherehas() 方法调用manpower
猜你喜欢
  • 1970-01-01
  • 2014-01-11
  • 2020-12-23
  • 2016-09-05
  • 2021-05-25
  • 2020-06-12
  • 2014-11-29
  • 2018-06-19
  • 2023-03-09
相关资源
最近更新 更多