問題 :我有一個名為product帶有一些列的表。其中之一是name。問題是 :示例條目:名稱:鹽粉。品名:辣椒粉問題當我查詢時, https://example.com/public/api/products?search=name%3Apowder 我得到 0 個結果。期待是回歸鹽粉和辣椒粉,因為“粉”一詞在兩者中都很常見?,F在,當我查詢 時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條經驗 獲得超8個贊
您可以使用$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 關注
- 147 瀏覽
添加回答
舉報
0/150
提交
取消