Laravel 5无法在foreach循环中生成where条件

时间:2018-08-17 13:02:39

标签: php laravel laravel-5

我想在foreach循环中生成where条件。

$query = \App\Model\ModelName::where('offerId', 3)->where('keyword', 'demo')->get();
$arr = [['setno', '=', '1'], ['deleted', '=', '0']];
$query->where(function($q) use ($arr){
  foreach($arr as $condition){
    $q->where($condition[0], $condition[1]);
  }
}

但是当我dd($query);时,它向我显示以下错误:

  

解析错误:语法错误,意外的'dd'(T_STRING),期望为','或')'

2 个答案:

答案 0 :(得分:4)

您缺少右括号:

$query->where(function($q) use ($arr){
  foreach($arr as $condition){
    $q->where($condition[0], $condition[1]);
  }
}); // <-- here

答案 1 :(得分:2)

您忘记了);通话的结束where

$query = \App\Model\ModelName::where('offerId', 3)->where('keyword', 'demo')->get();
$arr = [['setno', '=', '1'], ['deleted', '=', '0']];
$query->where(function($q) use ($arr){
    foreach($arr as $condition){
        $q->where($condition[0], $condition[1]);
    }
});
dd($query);