thinkphp 中 关联查询 like 查询失效

发布时间:2023年12月17日

controller:

   public function goodsList()
    {
        if (request()->isGet()) {
            //表单验证
            //调用发布
            $where = [];
            $goodname = $this->request->param('goodname', '');
            if(!empty($goodname)){
                    $where[] = ['name','like',$goodname];
            }
            return $this->logic->goodsList($where, $this->page, $this->size);
        }
        return '';
    }

logic:

  public function goodsList($search = null, $page = 1, $limit = 10)
    {
        $lists = Goods::when(($search && is_array($search)), function ($query) use ($search) {
            $query->where($search);
        })->alias('good')->field("good.id,good.type,good.name,good.photo,good.price,good.introduction,good.tag")->order('id', 'desc')->paginate($limit, false, ['page' => $page]);
        return $this->success("获取数据成功", $lists);
    }

测试的结果:

奇怪的是当我直接搜 :马抗? 无数据?

解决办法:加上通配符

    public function goodsList()
    {
        if (request()->isGet()) {
            //表单验证
            //调用发布
            $where = [];
            $goodname = $this->request->param('goodname', '');
            if(!empty($goodname)){
                $goodname = '%' . $goodname . '%';
                $where[] = ['good.name','like',$goodname];
            }
            return $this->logic->goodsList($where, $this->page, $this->size);
        }
        return '';
    }

文章来源:https://blog.csdn.net/qq_51110402/article/details/134971206
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。