問題 :我有一個(gè)名為product帶有一些列的表。其中之一是name。問題是 :示例條目:名稱:鹽粉。品名:辣椒粉問題當(dāng)我查詢時(shí), https://example.com/public/api/products?search=name%3Apowder 我得到 0 個(gè)結(jié)果。期待是回歸鹽粉和辣椒粉,因?yàn)椤胺邸币辉~在兩者中都很常見?,F(xiàn)在,當(dāng)我查詢 時(shí)https://example.com/public/api/products?search=name%3Asalt+powder,我得到Salt powder結(jié)果。這是我controller一直在嘗試在索引中實(shí)現(xiàn)的內(nèi)容: 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'); }有人可以幫助理解我應(yīng)該修改什么或哪個(gè)語句嗎?我嘗試過更改,但大多數(shù)嘗試都以錯(cuò)誤告終。任何幫助深表感謝。我可以分享你們可能有興趣了解的任何文件
1 回答

qq_遁去的一_1
TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用$request->query來獲取查詢字符串,并且因?yàn)槠渲邪皀ame:”,所以您需要提取搜索詞:
if ($queryString = $request->query('search')) {
[$column, $term] = explode(':', $queryString);
$products = Products::where('name', 'LIKE', "%{$term}%")
->orderBy('name')
->paginate(5);
}
注意:您是否缺少 else 子句?您似乎正在覆蓋 $products 變量。
- 1 回答
- 0 關(guān)注
- 161 瀏覽
添加回答
舉報(bào)
0/150
提交
取消