Php 多关系的Laravel查询
我需要计算发票状态-未结、已付款、过期 我的表格结构:Php 多关系的Laravel查询,php,laravel,laravel-5,laravel-5.5,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', functi
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函数而不是数据库原始查询来完成这项工作。请建议方法。此查询可用于分页:
Invoice::where('total_amount', '<=', function($query) {
$query->selectRaw('SUM(amount_received)')
->from('invoice_payments')
->where('invoice_id', DB::raw('invoices.id'));
})->paginate();
Invoice::where('total_amount','请发布您尝试过的内容以及遇到的问题。更新后,请检查。我认为您不能使用whereHas
子句,因为您需要比较两个不同表中的值。而whereHas
只执行嵌套子查询。您必须在中使用join
这-我关心的是获取发票的状态-没有意义。这意味着什么?我需要搜索所有发票,比如“已支付”状态。