我想在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),期望为','或')'
答案 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);