Laravel查询多个关系

时间:2018-04-13 02:03:04

标签: php laravel laravel-5 laravel-5.5

我需要计算发票状态 - 开票,付款,逾期。

我的表结构:

invoices:
- id
- custome_id
- total_amount
...

invoice_payments
- id
- invoice_id
- amount_received
...

尝试:

$records->whereHas('invoice', function($query) use ($val) {
    $query->whereHas('invoicePayments', function($_query){
        $_query->sum('amount_received');
    });
});

我担心的是获取发票的状态 - 如果invoice->sum('amount') > invoice->invoicePayment->sum('amount_received')

我想在whereHas函数的帮助下,而不是数据库原始查询。请建议如何做到这一点。

1 个答案:

答案 0 :(得分:1)

此查询适用于分页:

item