最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using array in where clause IN( ) Laravel - Stack Overflow

programmeradmin3浏览0评论

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
  • 1 Why not use DB::connection('DB')->select(/* ... */)->whereIn('code', $groupOfTopics)? – Tim Lewis Commented Mar 11 at 17:51
  • This project requires standardization, and in the rest of it the code is in raw queries. So I rather use it as well, to not be the guy that don't follow the standard – yellow_melro Commented Mar 12 at 11:14
  • 1 Having standards and being consistent is all well and good, but you're not using Laravel to it's full potential if you always use the DB 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 use DB::connection('db')->select(/* entire raw query */) over Model::select(...)->where(...)->get() (and similar). – Tim Lewis Commented Mar 12 at 12:46
  • I am an intern, I follow what has been told to me only haha – yellow_melro Commented Mar 12 at 17:02
  • 1 Haha ok, totally understandable. Again, not trying to get you to change your approach or anything, was simply very curious why that decision was made. "Because my senior(s) said so" is (unfortunately) a pretty common answer
发布评论

评论列表(0)

  1. 暂无评论