whereHas 不适用于 NULL 值

whereHas is not working on NULL value

in laravel 5.5 whereHas() 在值为 NULL 的地方不起作用。我与其他模型的关系是一对多的,我想从模型中选择值等于 NULL 的值。但不是返回特定值,而是返回结果

中的所有值
 plans = Plan::with('objectives')->with('objectives.keyResults')
           ->whereHas('objectives', function($query) {
               $query->whereNull('entity_id');
               $query->whereNull('quarter_id');
       })->where('companyKey', Auth::user()->companyKey)->get();

您必须指定两次约束:

$plans = Plan::with(['objectives' => function($query) {
        $query->whereNull('entity_id');
        $query->whereNull('quarter_id');
    }, 'objectives.keyResults'])
    ->whereHas('objectives', function($query) {
        $query->whereNull('entity_id');
        $query->whereNull('quarter_id');
    })->where('companyKey', Auth::user()->companyKey)
    ->get();

不需要双重约束,简化为:

$plans = Plan::whereHas('objectives', function($query) {
        $query->whereNull('entity_id');
        $query->whereNull('quarter_id');
    })->with(['objectives'])
       ->where('companyKey', Auth::user()->companyKey)
       ->get();