@chrisxhart your query make no sence. transactions_sum_amount is sum of all amounts. it can't be less then any of amount.
Also you can't use having statement without group statement (withSum works via subquery without grouping)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi all, trying to write a query below and saying the transactions_sum_amount isn't a column.
Context: A goal has many transactions, amount is per transaction. A goal also has an amount.
// Query throws an error
request()->user()->goals()->withSum('transactions', 'amount')->having('transactions_sum_amount', '>=', 'amount')->count()
// This query works as expected
request()->user()->goals()->withSum('transactions', 'amount')->having('transactions_sum_amount', '>=', 'amount')->get()
It looks like the count() isn't actually selecting the transactions_sum_amount column (as it only selects the count) for it to compare against. Anyway around this? Thanks.
Edit: How should I query against a computed aggregate if not this way?
Please or to participate in this conversation.