Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ehsanquddusi's avatar

Laravel Eloquent Query has issues, while as raw query works fine

I am writing a query in Laravel 5.5 Eloquent with for which toSql() returns

SELECT 
    `entity_type`, `entity_id`, SUM(amount) AS total
FROM
    `account_journals`
WHERE
    EXISTS( SELECT 
            *
        FROM
            `account_ledgers`
        WHERE
            `account_journals`.`ledger_id` = `account_ledgers`.`id`
                AND `name` = ?
                AND `account_ledgers`.`deleted_at` IS NULL)
GROUP BY `entity_type` , `entity_id`
HAVING `total` > ?

It shows error and doesn't run

SQLSTATE[42000]: Syntax error or access violation: 1463 Non-grouping field 
'total' is used in HAVING clause (SQL: select `entity_type`, `entity_id`, 
SUM(amount) AS total from `account_journals` where exists (select * from 
`account_ledgers` where `account_journals`.`ledger_id` = 
`account_ledgers`.`id` and `name` = Accounts Receivable and 
`account_ledgers`.`deleted_at` is null) group by `entity_type`, `entity_id` 
having `total` > 0)

If I run the same query directly, it is working fine. What can be the problem.

0 likes
2 replies
jlucia's avatar

Can you show your Eloquent query also?

ehsanquddusi's avatar
ehsanquddusi
OP
Best Answer
Level 2

Found the solution. Laravel 5.5 by default works in MySQL strict mode which is generating the error. Making strict to false in database config fixes the issue.

1 like

Please or to participate in this conversation.