Can you post the raw query here?
Sep 23, 2019
2
Level 1
Eloquent Model query results differently to same plain SQL query (using withCount, whereHas & having)
I have an invoice models which hasMany payments modes, now i want tho collect all Invoices which are paid. For that i need to compare the invoice->totals (=400$) with the sum(payment->amount).
$invoices = Invoice::withCount([
'payments as paymentsSum' => function($query) {
$query->select(DB::raw('SUM(amount)'));
}
])->whereHas('payments', function ($query) {
$query->completed();
})
->having('paymentsSum','<',400) //$, for testing purpose
->get();
This returns me invoices even if the invoice->totals is greater than 400$.
If i use toSql() instead of get() and copy the raw query into phpmyadmin i get only invoices back, which are smaller than 400$, as exptected.
Is there any hidden processing after the raw sql query when using the eloquent model?
Please or to participate in this conversation.