【发布时间】:2020-12-18 18:54:54
【问题描述】:
最终的用例是什么?
我的移动应用将发送https://example.com/public/api/classifiedsearch?search=name%3Aorange+apples+grapes+onions的查询
& 我希望结果包含来自products 表的数据,其中name 包含上面搜索字符串中的术语。我面临的问题是,我根本没有得到任何匹配的结果。我已经通过打印确认$terms(如下所述)确实能够将搜索中的值作为数组捕获,并且for循环的工作次数等于搜索字符串中的单词数。但没有返回任何结果。关于为什么不映射的任何提示/建议????
这是我在APIcontroller 中为classifiedsearch 所做的事情
public function index(Request $request)
{
try{
$queryString = $request->input('search',null);
$terms = explode(" ",$queryString);
// print_r($terms);
if ( !empty( $request->query('search')))
{
$products = Product::whereHas('store', function ($query) use ($terms)
{
foreach ($terms as $term)
{
// print_r("CHECKING FOR $term");
$query->where('name', 'like', '%' . $term . '%');
}
})->get();
}
else
{
$products = $this->productRepository->all();
}
} catch (RepositoryException $e) {
return $this->sendError($e->getMessage());
}
return $this->sendResponse($products->toArray(), 'Products retrieved successfully');
}
输出:
{"success":true,"data":[],"message":"Products retrieved successfully"}
有人可以帮忙吗??
【问题讨论】: