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

DimiFFT's avatar

Advanced query in the Query Builder

Hi,

How can i do the following query from the Laravel Query builder ?

SELECT * 
FROM  `my_table` 
WHERE  `col1` = 'value1'
AND  `cold2` =  'value2'
AND NOT (
    `col3` = 'value3'
    AND  `col4` = 'value4'
    AND  `col5` = 'value5'
)

Thank you in advance.

0 likes
2 replies
loshMiS's avatar
loshMiS
Best Answer
Level 16

Hey,

Well you should be able to accomplish this easily by converting the = to != and AND to OR for values inside the brackets. Something like this:

$data = \DB::table('my_table')
    ->where('col1', 'value1')
    ->where('col2', 'value2')
    ->where(function ($q) {
        $q->where('col3', '!=', 'value3')
        ->orWhere('col4', '!=', 'value4')
        ->orWhere('col5', '!=', 'value5')
    })->get();
1 like
DimiFFT's avatar

Perfect ! This is what i was looking for. Many thanks ;)

Please or to participate in this conversation.