I have 'hasManyThrough' relationship in model 'Customer' as shown below:
public function Assignments()
{
return $this->hasManyThrough(Assignment::class, Contract::class, 'customer_id', 'contract_id', 'customerId', 'contractId');
}
I'm using it like this:
if (is_array($customer)) {
foreach ($customer as $cus) {
$completedDosesNum += $cus->Assignments()
->whereDate('end_date', '<', $now)
->count();
$inProgDosesNum += $cus->Assignments()
->whereDate('start_date', '<=', $now)
->whereDate('end_date', '>=', $now)
->count();
$expBadges += $cus->Contracts()
->whereDate('end_validation_date', '<', $now)
->count();
// Assignments of Expired Contracts
$contractsEx = $cus->Contracts()
->whereDate('end_validation_date', '<', $now)
->get();
// Assignments of Alive Contracts
$contractsAl = $cus->Contracts()
->whereDate('end_validation_date', '>=', $now)
->whereDate('notification_date', '<=', $now)
->get();
$assignments = $cus->Assignments()->get();
foreach ($assignments as $assign) {
$over007D3M += $assign->AssignmentDetails()
->where([
['hp007_dose_result', '>=', $doseConf->min007]
])
->whereDate('date_dose_result', '>=', $now->subMonths(3))
->count();
$over10D3M += $assign->AssignmentDetails()
->where([
['hp10_dose_result', '>=', $doseConf->min10]
])
->whereDate('date_dose_result', '>=', $now->subMonths(3))
->count();
$over007D6M += $assign->AssignmentDetails()
->where([
['hp007_dose_result', '>=', $doseConf->min007]
])
->whereDate('date_dose_result', '>=', $now->subMonths(6))
->count();
$over10D6M += $assign->AssignmentDetails()
->where([
['hp10_dose_result', '>=', $doseConf->min10]
])
->whereDate('date_dose_result', '>=', $now->subMonths(6))
->count();
$over007D9M += $assign->AssignmentDetails()
->where([
['hp007_dose_result', '>=', $doseConf->min007]
])
->whereDate('date_dose_result', '>=', $now->subMonths(9))
->count();
$over10D9M += $assign->AssignmentDetails()
->where([
['hp10_dose_result', '>=', $doseConf->min10]
])
->whereDate('date_dose_result', '>=', $now->subMonths(9))
->count();
$over007D12M += $assign->AssignmentDetails()
->where([
['hp007_dose_result', '>=', $doseConf->min007]
])
->whereDate('date_dose_result', '>=', $now->subMonths(12))
->count();
$over10D12M += $assign->AssignmentDetails()
->where([
['hp10_dose_result', '>=', $doseConf->min10]
])
->whereDate('date_dose_result', '>=', $now->subMonths(12))
->count();
}
foreach ($contractsEx as $value) {
$uWorkers += $value->AssignmentDetails()->count();
}
foreach ($contractsAl as $value) {
$mWorkers += $value->AssignmentDetails()->count();
}
dump($inProgDosesNum);
}
} else {
$completedDosesNum = $customer->Assignments()
->whereDate('end_date', '<', $now)
->count();
$inProgDosesNum = $customer->Assignments()
->whereDate('start_date', '<=', $now)
->whereDate('end_date', '>=', $now)
->count();
$expBadges = $customer->Contracts()
->whereDate('end_validation_date', '<', $now)
->count();
// Assignments of Expired Contracts
$contractsEx = $customer->Contracts()
->whereDate('end_validation_date', '<', $now)
->get();
// Assignments of Alive Contracts
$contractsAl = $customer->Contracts()
->whereDate('end_validation_date', '>=', $now)
->whereDate('notification_date', '<=', $now)
->get();
$assignments = $customer->Assignments()->get();
foreach ($assignments as $assign) {
$over007D3M += $assign->AssignmentDetails()
->where([
['hp007_dose_result', '>=', $doseConf->min007]
])
->whereDate('date_dose_result', '>=', $now->subMonths(3))
->count();
$over10D3M += $assign->AssignmentDetails()
->where([
['hp10_dose_result', '>=', $doseConf->min10]
])
->whereDate('date_dose_result', '>=', $now->subMonths(3))
->count();
$over007D6M += $assign->AssignmentDetails()
->where([
['hp007_dose_result', '>=', $doseConf->min007]
])
->whereDate('date_dose_result', '>=', $now->subMonths(6))
->count();
$over10D6M += $assign->AssignmentDetails()
->where([
['hp10_dose_result', '>=', $doseConf->min10]
])
->whereDate('date_dose_result', '>=', $now->subMonths(6))
->count();
$over007D9M += $assign->AssignmentDetails()
->where([
['hp007_dose_result', '>=', $doseConf->min007]
])
->whereDate('date_dose_result', '>=', $now->subMonths(9))
->count();
$over10D9M += $assign->AssignmentDetails()
->where([
['hp10_dose_result', '>=', $doseConf->min10]
])
->whereDate('date_dose_result', '>=', $now->subMonths(9))
->count();
$over007D12M += $assign->AssignmentDetails()
->where([
['hp007_dose_result', '>=', $doseConf->min007]
])
->whereDate('date_dose_result', '>=', $now->subMonths(12))
->count();
$over10D12M += $assign->AssignmentDetails()
->where([
['hp10_dose_result', '>=', $doseConf->min10]
])
->whereDate('date_dose_result', '>=', $now->subMonths(12))
->count();
}
foreach ($contractsEx as $value) {
$uWorkers += $value->AssignmentDetails()->count();
}
foreach ($contractsAl as $value) {
$mWorkers += $value->AssignmentDetails()->count();
}
}
in the first block of 'if', the 'Assignments()' method not working as expected and the variable '$completedDosesNum' contains the count of the first item of the loop!!?
but in the second block it works fine and gives me the correct count of 'Assignments()' for the selected customer.
Thanks in advance.