找不到Larvavel模型和参数错误

时间:2017-06-21 06:35:36

标签: php model laravel-5.4 php-7

我是PHP和Laravel的新手,我得到了一个我无法做出头脑或者尾巴的恐怖。

  public function filtered($col, $sort = null, $search = null, $ordering='desc')
    {
        $field = $this->table . '.' . $col ; 

        Log::info('BaseModel::filtered->' . $field) ;

        $data = $this;
        // check if search variable not empty
        if ($search != null)
        {
            $data = $data->where(function ($query) use ($search){
                return $query->where($field,'like','%'.$search.'%') ;
            });

        // check if sort variable not empty
        if ($sort != null)
           {
           $sorts = explode('|', $sort);

           $data = $data->orderBy($sorts[0],$sorts[1]);
           }
        }
 ...

上面的代码给出了错误:Undefined variable: field。从日志输出我得到:

[2017-06-21 06:32:25] local.INFO: BaseModel::filtered->organisation.name

我也试过按$this->field调用字段变量,也失败了。如果我直接引用$col参数,我也会得到相同的错误。但$search$sort都很好。

1 个答案:

答案 0 :(得分:1)

$data = $data->where(function ($query) use ($search, $field) { //Add extra parameters inside your use same as search.
    return $query->where($field,'like','%'.$search.'%') ;
});