简体   繁体   English

使用whereIn(laravel 5.3)为foreach()laravel提供的参数无效

[英]Invalid argument supplied for foreach() laravel using whereIn (laravel 5.3)

I am getting ids using jquery in my controller its as 我在我的控制器中使用jquery获取id

if(!empty($request->input('ids'))) {
    $ids = $request->input('ids');
    }else{
                $ids = null;
            }

I try dd($ids) 我试试dd($ids)

Output is as in my console 输出与我的控制台一样

array:3 [
  0 => "1"
  1 => "2"
  2 => "on"
]

When i pass ids to my query as 当我将id传递给我的查询时

Query 询问

$student_ids = DB::table('students')
             ->orderBy('name', 'asc')
              ->where('group_id', '<>', 0)
              ->whereIn('students.id','=', $ids)
              ->pluck('id');

Its not working 它不起作用

Error is : Invalid argument supplied for foreach 错误是:为foreach提供的参数无效

Please help where I am wrong. 请帮我错。 Thanks 谢谢

You should change this: 你应该改变这个:

->whereIn('students.id','=', $ids)

To this: 对此:

->whereIn('students.id', $ids)

Because the second parameter should be iterable of IDs, but you're passing a string = . 因为第二个参数应该是可迭代的ID,但是你传递了一个string = Also, make sure, you're passing an array or collection of IDs. 此外,请确保您传递的是数组或ID集合。

Change: 更改:

where('students.id', '=', $ids)

To: 至:

whereIn('students.id', $ids);

Like others have mentioned update the whereIn parameter. 像其他人一样提到更新whereIn参数。 Also simplify your request input like so. 同样简化您的请求输入。

$ids = $request->input('ids', []);

$student_ids = DB::table('students')
    ->orderBy('name', 'asc')
    ->where('group_id', '<>', 0)
    ->whereIn('students.id', $ids)
    ->pluck('id');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM