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

I am trying to use an array in the WHERE clause with IN () in Laravel, but it's not working as exp

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

    发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744840757a4596541.html

相关推荐

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

    I am trying to use an array in the WHERE clause with IN () in Laravel, but it's not working as exp

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信