TransWikia.com

Laravel whereHas Filter sub relation

Stack Overflow Asked by Bryant Tang on February 1, 2021

Is that any method to filter the employees and the manpower table record does not return also?

$result = Pwra::with('purchaseOrder', 'manpower')
->where('pwra_dt', $date)
->where('time_session', $session)
->whereHas('manpower.employees', function ($q) {
    $q->where('status', 1);
})
->get();

Pwra Class

public function manpower()
{
    return $this->hasMany('AppModelsManpower', 'pwra_uuid', 'pwra_uuid');
}

Manpower Class

public function employee()
{
    return $this->hasOne('AppModelsEmployees', 'employees_uuid', 'employees_uuid')->where('status', 1);
}

What I expected is: When the employees Status = 0, it will not return any record even manpower.

One Answer

you can try 2 level 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' it will not work

Answered by Kamlesh Paul on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP