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 '';
}