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

monstajamss's avatar

Laravel SQL Query Problem

I have this SQL Query Statement below

SELECT DISTINCT parent_category.id, parent_category.description FROM category AS sub_category INNER JOIN category AS parent_category ON (sub_category.parent_category_id = parent_category.id) WHERE sub_category.client_id = '19' AND sub_category.id IN (SELECT category_id FROM `after_hours_yesterday_category` WHERE cost_in_hours + cost_out_of_hours > 0 AND building_id = 52)

But my problem is the + symbol that's present in the query i am trying to write it in laravel query builder like thus

 $other = $this->electricityConnections->select('category_id')
        ->from('after_hours_yesterday_category')
        ->where(\DB::raw('cost_in_hours'), '+', (\DB::raw('cost_out_of_hours ')), 0)
        ->where('building_id', '=', 52)
        ->toSql();
        dd($other);

I get this after diedump

select `category_id` from `after_hours_yesterday_category` where 0 cost_in_hours = ? `building_id` = ?

which i feel is not correct.

Any help will be appreciated

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You are misusing the raw Expressions. You can use whereRaw like this

->whereRaw('cost_in_hours + cost_out_of_hours > 0')
1 like

Please or to participate in this conversation.