【发布时间】:2020-09-16 13:31:20
【问题描述】:
我正在尝试从事务表中查询金额小于分配表中针对该事务行的分配总和的行。这行得通。它给了我所有带有“allocated_amount”附加字段的交易查询。
$all_transactions = Transaction::
withCount([
'allocations AS amount_allocated' => function ($query) {
$query->select(
DB::raw("CAST(SUM(amount) AS INTEGER)"
));
}
])
->where('contact_type','supplier')
->where('contact_id',$ehead->supplier_id)
->get()->toArray();
结果
0 => array:12 [▼
"id" => 4
"amount" => 10000
"created_by" => 1
"amount_allocated" => 7500
]
1 => array:12 [▼
"id" => 5
"amount" => 10000
"created_by" => 1
"amount_allocated" => 10000
]
但我不希望出现第二个结果,因为数量 = 分配的数量。如何修复此查询?
尝试使用:
->where('amount','>','allocated_amount')
但可能是因为它额外添加了字段,抛出一个错误,说分配的字段不存在。我在查询中需要它,而不是单独添加。
【问题讨论】:
标签: laravel eloquent laravel-7