Laravel 5.4 whereNotIn显然不起作用

时间:2017-08-18 09:39:33

标签: laravel-5 eloquent

我在控制器中有逻辑,它构建一个名为$ exclude的数组。

使用dd代替$ exclude我得到:

array:4 [▼
  0 => 2
  1 => 3
  2 => 4
  3 => 5
]

这是正确的。

我想从结果中排除那些,所以我有:

$potype = DB::table('potypes')
                ->whereNotIn('id',[$exclude])
                ->get();

但是当我运行查询时,除了数组中的第一个项外,这些项都包含在内。所以我用

启用了查询日志
DB::enableQueryLog();

然后跑

dd(DB::getQueryLog());

结果为

array:1 [▼
  0 => array:3 [▼
    "query" => "select * from `potypes` where `id` not in (?)"
    "bindings" => array:4 [▼
      0 => 2
      1 => 3
      2 => 4
      3 => 5
    ]
    "time" => 0.67
  ]
]

该表有8条记录,但运行查询返回7,只省略了第一个列表:

Collection {#621 ▼
  #items: array:7 [▼

如果我使用implode

$ ex = implode(',',$ exclude) 并将查询更改为      - > whereNotIn(' ID',[$ EX]) 我得到了相同的结果 - 只有列表中第一个被忽略的7个项目。

这是一个雄辩的错误还是我?

2 个答案:

答案 0 :(得分:3)

删除[ ]并再次检查:

$potype = DB::table('potypes')
                ->whereNotIn('id',$exclude)
                ->get();

答案 1 :(得分:-2)

好吧这很简单,穆罕默德的评论指出了我正确的方向。

由于$excludearray,我将get更改为:

$potype = DB::table('potypes')
            ->whereNotIn('id',$exclude)
            ->get();

它工作正常!

相关问题