I am trying to use an array in the WHERE clause with IN () in Laravel, but it's not working as expected. Here’s my current code:
$groupOfTopics = [46, 51, 167, 176, 177, 181, 185, 270, 323, 328, 350];
$Data = DB:: connection('DB')->select(
"Select
....
WHERE code IN (?)", [$groupOfTopics],);
However, this does not return the expected results.
How can I correctly pass an array into the IN clause in Laravel raw queries?
I am trying to use an array in the WHERE clause with IN () in Laravel, but it's not working as expected. Here’s my current code:
$groupOfTopics = [46, 51, 167, 176, 177, 181, 185, 270, 323, 328, 350];
$Data = DB:: connection('DB')->select(
"Select
....
WHERE code IN (?)", [$groupOfTopics],);
However, this does not return the expected results.
How can I correctly pass an array into the IN clause in Laravel raw queries?
Share Improve this question asked Mar 10 at 14:42 yellow_melroyellow_melro 255 bronze badges 5
DB::connection('DB')->select(/* ... */)->whereIn('code', $groupOfTopics)
? – Tim Lewis Commented Mar 11 at 17:51DB
facade... Do you also not use Models and Relationships? I'd be very interested to hear why this choice was made; I can't really think of any good reasons you'd useDB::connection('db')->select(/* entire raw query */)
overModel::select(...)->where(...)->get()
(and similar). – Tim Lewis Commented Mar 12 at 12:46