我正在尝试从事务表中查询行,其中行数小于分配表中该事务行的分配总和。这工作一半。它给了我所有带有“ 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')
但是可能是因为它是另外添加的字段,所以抛出错误,指出已分配的字段不存在。我在查询中需要它,而不是单独添加它。
我正在尝试从事务表中查询行,其中行数小于分配表中该事务行的分配总和。这工作一半。它给了我所有...
嗯,在这种情况下,您可以简单地使用map()