问题 :我有一个名为product带有一些列的表。其中之一是name。问题是 :示例条目:名称:盐粉。品名:辣椒粉问题当我查询时, https://example.com/public/api/products?search=name%3Apowder 我得到 0 个结果。期待是回归盐粉和辣椒粉,因为“粉”一词在两者中都很常见。现在,当我查询 时https://example.com/public/api/products?search=name%3Asalt+powder,我得到Salt powder结果。这是我controller一直在尝试在索引中实现的内容: public function index(Request $request) { if (Query::has('search')) { ------>>> I know that something is terribly wrong here. $queryString = Query::get('search'); $products = Products::where('name', 'LIKE', "%$queryString%")->orderBy('name')->paginate(5); } try{ $this->productRepository->pushCriteria(new RequestCriteria($request)); $this->productRepository->pushCriteria(new LimitOffsetCriteria($request)); $this->productRepository->pushCriteria(new ProductsOfFieldsCriteria($request)); if($request->get('trending',null) == 'week'){ $this->productRepository->pushCriteria(new TrendingWeekCriteria($request)); } else{ $this->productRepository->pushCriteria(new NearCriteria($request)); } $products = $this->productRepository->all(); } catch (RepositoryException $e) { return $this->sendError($e->getMessage()); } return $this->sendResponse($products->toArray(), 'Products retrieved successfully'); }有人可以帮助理解我应该修改什么或哪个语句吗?我尝试过更改,但大多数尝试都以错误告终。任何帮助深表感谢。我可以分享你们可能有兴趣了解的任何文件
1 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
您可以使用$request->query来获取查询字符串,并且因为其中包含“name:”,所以您需要提取搜索词:
if ($queryString = $request->query('search')) {
[$column, $term] = explode(':', $queryString);
$products = Products::where('name', 'LIKE', "%{$term}%")
->orderBy('name')
->paginate(5);
}
注意:您是否缺少 else 子句?您似乎正在覆盖 $products 变量。
- 1 回答
- 0 关注
- 105 浏览
添加回答
举报
0/150
提交
取消