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

mabdullahsari's avatar

Make the query builder interpret a value as a column name

(Context: query scope where $value is either true or false)

I'd like to do this:

return $query
    ->withCount('persons')
    ->having('persons_count', $value ? '>=' : '<', 'allowed_submissions');

But have to use raw because 'allowed_submissions' is interpreted as a value

return $query
    ->withCount('persons')
    ->havingRaw('persons_count ' . ($value ? '>= ' : '< ') . $this->qualifyColumn('allowed_submissions'));

Is there something to make the first option work or do I have to settle with the second one?

0 likes
1 reply
kalemdzievski's avatar

For the very same example there is a great functionality for where statements. There is a whereColumn method for that purpose.

But as i can see in the Query Builder API there is no such functionality for having, so you will have to stick the havingRaw option, which is actually just fine.

Please or to participate in this conversation.