代码之家  ›  专栏  ›  技术社区  ›  Niladri Banerjee - Uttarpara

Laravel 5无法在foreach循环中生成where条件[复制]

  •  0
  • Niladri Banerjee - Uttarpara  · 技术社区  · 6 年前

    我想在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); 它显示了以下错误:

    2 回复  |  直到 6 年前
        1
  •  4
  •   Oliver Maksimovic    6 年前

    缺少右括号:

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

    你忘了关门 ); 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);