1

I want to generate where conditions within a foreach loop.

$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]);
  }
}

But when I dd($query); it show me the below error:

Parse error: syntax error, unexpected 'dd' (T_STRING), expecting ',' or ')'

Teoman Tıngır
  • 2,766
  • 2
  • 21
  • 41
  • `where` can just take that array itself, you dont have to iterate and add the wheres .... `where($arr)` – lagbox Aug 17 '18 at 13:14

2 Answers2

4

You are missing the closing bracket:

$query->where(function($q) use ($arr){
  foreach($arr as $condition){
    $q->where($condition[0], $condition[1]);
  }
}); // <-- here
Oliver Maksimovic
  • 3,204
  • 3
  • 28
  • 44
2

You forgot the closing ); for the where call.

$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);
jrenk
  • 1,387
  • 3
  • 24
  • 46