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

klik's avatar
Level 1

Question mark in raw sql query cause problem

Hi. I have a problem in my raw query select. Example query:

DB::select( DB::raw( 
"SELECT * FROM event e WHERE e.repeat_every->'daysInWeekRepeat' ? :dayNow" 
),
 ['dayNow' => 'mon' ]
);

(Original query is much more complicate this is only example.)

Column "repeat_every" is a jsonb column, and I have to use question mark to search param value in array. This question mark cause a problem "invalid paremeter number". In output insted of question mark I get first param passed to query.

SO how to write this query to ignore question mark as a placeholder?

Thank you.

0 likes
6 replies
muzafferdede's avatar
DB::select('*')->from('event')->where('event.repeat_every->daysInWeekRepeat','mon')
rodrigo.pedra's avatar

Is it postgres?

According to this link:

https://stackoverflow.com/a/36177073

You can use jsonb_exists to avoid the question mark:

DB::select("SELECT * FROM event e WHERE jsonb_exists(e.repeat_every->'daysInWeekRepeat', :dayNow)", ['dayNow' => 'mon']);

Also you don't need to wrap the query into DB::raw when running directly in the DB façade select method.

klik's avatar
Level 1

Works fine. Thank you.

1 like

Please or to participate in this conversation.