seqond's avatar

Pagination with having clause

Laravel 6 please help.

pays its a hasManyThrough relation.

$orders->withCount(['pays as pay_sum' => function (Builder $q) {
                        return $q->select(DB::raw('SUM(invoice_pays.sum)'));
                    }])->havingRaw('pay_sum >= `orders`.`amount`')
                     //->get(); Everything works well
                     ->paginate(10);

I get an error

Column not found: 1054 Unknown column 'orders.amount' in 'having clause'

Is there any way to solve this problem?

0 likes
1 reply
jlrdw's avatar

HAVING is usually used after GROUP BY, see if a WHERE clause works. Usually on something like that I actually use some trial and error to solve.

Please or to participate in this conversation.