Laravel4:WhereHas Eloquent Issue(嵌套)。回调函数错误

时间:2014-05-09 18:47:37

标签: orm laravel-4 eloquent

我尝试了一个搜索功能,其中唯一的字段是<input type='text' />,它将被剥离到array()然后传递给whereLoop。

static function generateSearch($fields, $queryString)
            {
                    return function($query) use($queryString, $fields)
                            {
                                    foreach($fields as $field) {
                                            $query = $query->orWhere($field, 'like', $queryString);
                                    }

                                    $query = $query->whereHas('category', function($_query) use ($queryString)
                                    {
                                            $_query->where('name','like',$queryString);
                                    });

                            };



            }

            public static function search($query)
            {

                    $searchBits = explode(' ', $query);                    

                    $query  = Lead::with(array('user', 'category'));
                    $ctr    = 0;

                    if(Category::whereIn('name', $searchBits)->count() != 0) {

                            $query = $query->whereHas('category', function($query) use ($searchBits)
                            {
                                    $ctr = 0;
                                    foreach($searchBits as $bit) {
                                            $bit            = "%".$bit."%";
                                            $callback       = "orWhere";
                                            $queryFunc = Lead::generateSearch(array('name'), $bit);

                                            if($ctr == 0) {
                                                    $callback = "where";
                                            }

                                            $query = $query->$callback($queryFunc);
                                    }
                            });

                    }else {

                            foreach($searchBits as $bit) {

                                    $bit            = "%".$bit."%";
                                    $callback       = "orWhere";
                                    $queryFunction = Lead::generateSearch(array('name', 'website', 'name', 'email'), $bit);

                                    if($ctr == 0) {
                                            $callback = "where";
                                    }
                                    $query = $query->$callback($queryFunction);

                                    $ctr++;
                            }

                    }
                    $query = $query->orderBy('id','desc');

                    return $query;
            }

类别目前只有一行:“ - 热”

  • 如果我输入任何关键字,它会直接转到generateSearch()
  • 但如果我输入“hot”,则会发送错误
  • 调用未定义的方法 的 Illuminate\Database\Query\Builder::category()

有人知道发生了什么吗?

1 个答案:

答案 0 :(得分:0)

发现错误。在深入了解callstack之后。我不应该添加

                $query = $query->whereHas('category', function($_query) use ($queryString) 
                {
                    $_query->where('name','like',$queryString);
                });

在generateSearch()内部,或者我应该为它创建另一个函数。它也被类别回调调用。