duancheng3342 2017-06-21 06:35
浏览 68
已采纳

未找到Laravel模型和参数错误

I'm very new to PHP and Laravel, I'm getting an eror that I can't make head or tail of.

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

The code above is giving the error: Undefined variable: field. From the Log output I get this:

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

I've tried calling the field variable by $this->field as well, also fails. I also get the same error if i reference the $col parameter directly. Yet both $search and $sort are fine.

  • 写回答

1条回答 默认 最新

  • douxian0008 2017-06-21 06:41
    关注
    $data = $data->where(function ($query) use ($search, $field) { //Add extra parameters inside your use same as search.
        return $query->where($field,'like','%'.$search.'%') ;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题