首页 > 解决方案 > 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 时,它不会返回任何记录,甚至人力。

标签: laravel

解决方案


你可以试试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'不起作用


推荐阅读