I have a required to calculate the invoices status - Open, Paid, Overdue.
My table structure:
invoices:
- id
- custome_id
- total_amount
...
invoice_payments
- id
- invoice_id
- amount_received
...
Trying:
$records->whereHas('invoice', function($query) use ($val) {
$query->whereHas('invoicePayments', function($_query){
$_query->sum('amount_received');
});
});
My concern is to get the status for the invoice
- if invoice->sum('amount') > invoice->invoicePayment->sum('amount_received').
I want to do it with the help of whereHas functions not with database raw queries. Please suggest the way to do it.
whereHasclause, because you need to compare the value from two different tables. Whereas awhereHasjust executes a nested sub query. You'll have to use ajoininstead.