不起作用的代码: ( where(['category_id' => $item['id']]) 没起作用 ) 传递参数
$model->where('product_name', 'like', "%$productName%") 不起作用
public function cateProductPage() { $builder = new Builder(new CategoryModel()); $builder->setDefaultSort('sort,ASC'); $builder->setFilter(['pid' => -1]); $lists = $builder->selectQuery(function (BaseQuery $query) { })->select(); $productName = $this->request->post('product_name', ''); $model = new ProductModel(); if ($lists->toArray()) { foreach ($lists as &$value) { // 二级分类 名 $value['c_names'] = (new CategoryModel())->where(['pid' => $value['id']])->field('id,name')->select(); // 二级分类商品 foreach ( $value['c_names'] as &$item){ $item['good'] = $model->where(['category_id' => $item['id']]); if (!empty($productName)) { $model->where('product_name', 'like', "%$productName%")->select(); } $item['good'] = $model->select(); // 提取标签中的 ‘约500g/份 约10-20 只' 约的展示 与 标签不展示在一起了 foreach ($item['good'] as &$index) { $pattern = '/约[^,]*/'; preg_match_all($pattern, implode(', ',$index['tags']), $matches); $index['tag'] = explode(',', implode(",", $matches[0])); } } $where = []; $user = $this->request->getUser(); if ($user) { $where[] = ['is_view', 'find in set', $user['user_type']]; } $pro = (new ProductModel())->with(['sku', 'ranks', 'category'])->whereRaw('concat(\' \',category_id,\' \') like "% ' . $value['id'] . ' %"')->where('status', '10')->where($where)->order([ 'sort' => 'ASC', 'id' => 'DESC', ])->select(); $value['product'] = $pro; } unset($value); unset($item); unset($index); } return $lists; }
起作用的代码:
public function cateProductPage() { $builder = new Builder(new CategoryModel()); $builder->setDefaultSort('sort,ASC'); $builder->setFilter(['pid' => -1]); $lists = $builder->selectQuery(function (BaseQuery $query) { })->select(); $productName = $this->request->post('product_name', ''); $model = new ProductModel(); if ($lists->toArray()) { foreach ($lists as &$value) { // 二级分类 名 $value['c_names'] = (new CategoryModel())->where(['pid' => $value['id']])->field('id,name')->select(); // 二级分类商品 foreach ( $value['c_names'] as &$item){ $item['good'] = $model->where(['category_id' => $item['id']])->where(function ($query) use($productName) { if(!empty($productName)){ $query->where('product_name', 'like', "%$productName%"); } })->select(); // 提取标签中的 ‘约500g/份 约10-20 只' 约的展示 与 标签不展示在一起了 foreach ($item['good'] as &$index) { $pattern = '/约[^,]*/'; preg_match_all($pattern, implode(', ',$index['tags']), $matches); $index['tag'] = explode(',', implode(",", $matches[0])); } } $where = []; $user = $this->request->getUser(); if ($user) { $where[] = ['is_view', 'find in set', $user['user_type']]; } $pro = (new ProductModel())->with(['sku', 'ranks', 'category'])->whereRaw('concat(\' \',category_id,\' \') like "% ' . $value['id'] . ' %"')->where('status', '10')->where($where)->order([ 'sort' => 'ASC', 'id' => 'DESC', ])->select(); $value['product'] = $pro; } unset($value); unset($item); unset($index); } return $lists; }
还没有评论,来说两句吧...