laravel先生不工作

时间:2014-05-24 05:29:36

标签: laravel

我的高级语言功能没有给出任何语法错误,但它甚至没有工作。它的显示。我按照这个例子http://laravel.com/docs/queries#advanced-wheres

写了这个函数
public function getData($wheres1 = array(),$wheres2 = array(),$wheres3 = array(),$wheres4 = array(),$wheres5 = array(),$wheres6 = array(),$wheres7 = array())
    {
    $query = Listing::query();
    $result = array();

      $query = $query->where(function($q){

      if(!empty($wheres1)){

        $count=0;
       foreach($wheres1 as $where){ 
       if($count==0)
       { $q = $q->where($where['field'], $where['operator'], $where['value']);
               $count++;
       }
       else
         $q = $q->orWhere($where['field'], $where['operator'], $where['value']);

       }
     }

      })->where(function($q){

            if(!empty($wheres2)){

        $count=0;
       foreach($wheres2 as $where){ 
       if($count==0)
       { $q = $q->where($where['field'], $where['operator'], $where['value']);
               $count++;
       }
       else
         $q = $q->orWhere($where['field'], $where['operator'], $where['value']);

       }
     }



      })->where(function($q){

        if(!empty($wheres3)){

        $count=0;
       foreach($wheres3 as $where){ 
       if($count==0)
       { $q = $q->where($where['field'], $where['operator'], $where['value']);
               $count++;
       }
       else
         $q = $q->orWhere($where['field'], $where['operator'], $where['value']);

       }
     }


      });


    $result = $query->orderBy('id', 'desc')->paginate(3);
    return $result;

    }    

2 个答案:

答案 0 :(得分:2)

您需要为每个use

添加where
$query = $query->where(function($q) use ($wheres1) {
....
}

答案 1 :(得分:1)

您可以尝试这种方法:

$query = Listing::query();
if(is_array($wheres1) && count($wheres1)) {
    $first = array_shift($wheres1);
    $query->where($first['field'], $first['operator'], $first['value']);
    if(count($wheres1)) {
        foreach($wheres1 as $where) {
            $query->orWhere($wheres['field'], $wheres['operator'], $wheres['value']);
        }
    }
}

现在为wheres的其余部分重复相同的过程,例如($wheres2):

if(is_array($wheres2) && count($wheres2)) {
    $first = array_shift($wheres2);
    $query->where($first['field'], $first['operator'], $first['value']);
    if(count($wheres2)) {
        foreach($wheres2 as $where) {
            $query->orWhere($wheres['field'], $wheres['operator'], $wheres['value']);
        }
    }
}

最后:

return $query->orderBy('id', 'desc')->paginate(3);