Laravel 拉威尔儿童口才过滤器

Laravel 拉威尔儿童口才过滤器,laravel,eloquent,Laravel,Eloquent,我有以下关系: 顾客 ->有许多(合同) 现在,我希望获得所有客户,包括具有给定条件的所有合同: $c = Customer::with('contracts','contracts.service','contracts.contractpos') ->whereHas('contracts', function ($query) { $query ->where('invoice_next','=',$this->invoice_ne

我有以下关系:

顾客 ->有许多(合同)

现在,我希望获得所有客户,包括具有给定条件的所有合同:

$c = Customer::with('contracts','contracts.service','contracts.contractpos')
    ->whereHas('contracts', function ($query) {
        $query
        ->where('invoice_next','=',$this->invoice_next)
        ->groupBy('customer_id');
    })
    ->orderBy('company')
    ->get();
但是,我得到了所有合同已满且满足条件的客户(invoice_next=$this->invoice.next),但也得到了所有与条件不匹配的合同

因此,过滤客户是可行的,但我会得到所有客户的合同,而不仅仅是到期的合同。我在哪里犯错误

子查询中的“where”条件似乎只适用于客户,而不过滤子项(合同)

谢谢你给我任何正确方向的提示

问候 eXe

尝试以下操作:

$c = Customer::with(['contracts' => function($query){
          $query->where('invoice_next','=',$this->invoice_next)
          ->with(['service', 'contractpos'])
          ->groupBy('customer_id');
     }])
    ->whereHas('contracts', function ($query) {
        $query->where('invoice_next','=',$this->invoice_next);            
    })
    ->orderBy('company')
    ->get();
试试这个:

$c = Customer::with(['contracts' => function($query){
          $query->where('invoice_next','=',$this->invoice_next)
          ->with(['service', 'contractpos'])
          ->groupBy('customer_id');
     }])
    ->whereHas('contracts', function ($query) {
        $query->where('invoice_next','=',$this->invoice_next);            
    })
    ->orderBy('company')
    ->get();

你好相同的结果集。它似乎正确地过滤了客户,但如果其中一份合同有效,他只会返回该客户的所有合同。太棒了!我只需要删除->groupBy,因为它抛出了一个错误。但是,由于这个雄辩的模型,这是不需要的。非常感谢你!你好相同的结果集。它似乎正确地过滤了客户,但如果其中一份合同有效,他只会返回该客户的所有合同。太棒了!我只需要删除->groupBy,因为它抛出了一个错误。但是,由于这个雄辩的模型,这是不需要的。非常感谢你!