我有一个名为“ Recibo”的模型,在此模型上,我有一个hasMany关系,如下所示:
public function recaudarRecibo(){
return $this->hasMany('App\RecaudaRecibo','ID_RECIBO','ID_RECIBO');
}
因为“ RECIBO”(收据)可能在一个或多个“ RECAUDACION”(收入)中,所以“ RECAUDACION”可以支付一个或多个“ CUOTA”(应付款),因此我使用的型号为“ RecaudaRecibo”另一个关系:
public function compromisoCuotaPago(){
return $this->hasMany('App\CompromisoCuotaPago', 'ID_RECAUDA', 'ID_RECAUDA')
->where('SEC_RECAUDA', $this->SEC_RECAUDA)
->where('ID_RECIBO', $this->ID_RECIBO);
}
这是有趣的部分:目前,我正在开发报告,这是我现在雄辩的查询:
$recaudaciones = Recibo::whereHas('recaudarRecibo', function($q) use ($anio){
$q->whereRaw("YEAR(FEC_PAGO) = $anio")
->whereRaw("IND_DIR_EXT = 'D'")
->doesntHave('compromisoCuotaPago')
->where("COD_ESTADO_PAGO", 1);
})->where("COD_TIPO_APORTE", 1)
->where("ID_LINEA_INGRESO", $lineaIngreso->ID_LINEA_INGRESO)
->get();
问题是whereHas('recaudaRecibo')
不能与关系compromisoCuotaPago
关联,但是在我打印查询时会发生这种情况:
select *
from `M_RECIBO`
where exists (
select *
from `R_RECAUDA_RECIBO`
where `M_RECIBO`.`ID_RECIBO` = `R_RECAUDA_RECIBO`.`ID_RECIBO` and YEAR(FEC_PAGO) = 2019
AND MONTH(FEC_PAGO) = 10 and IND_DIR_EXT = 'D' and not exists
(select *
from `R_COMPROMISO_CUOTA_PAGO`
where `R_RECAUDA_RECIBO`.`ID_RECAUDA` = `R_COMPROMISO_CUOTA_PAGO`.`ID_RECAUDA`
and `SEC_RECAUDA` is null and `ID_RECIBO` is null)
and `COD_ESTADO_PAGO` = 1)
and `COD_TIPO_APORTE` = 1 and YEAR(FEC_VCTO) = (2019 - 1)
and `ID_LINEA_INGRESO` = 4;
因此,这种关系是不正确的,当我从对象中使用它时很好,但是当我进行这样的查询时,它给出了null。有什么想法吗?