问题:
我试图在一个查询中搜索多个关键词,但由于某种原因,我总是会遇到这样的错误:
[2020-08-29 03:50:02] development.ERROR: Class 'AppHttpControllersAPIPost' not found {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Class 'App\Http\Controllers\API\Post' not found at /home2/examplehat/public_html/shopper/app/Http/Controllers/API/ClassifiedSearchAPIController.php:78)
[stacktrace]
这是我的控制器的索引功能:
public function index(Request $request)
{
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));
}
$queryString = $request->query;
if ($queryString = $request->query('search')) {
// [$column, $term] = explode(':', $queryString);
$terms = explode(" ", request('q'));
$products = Product::query()
->whereHas('store', function ($query) use ($terms) {
foreach ($terms as $term) {
// Loop over the terms and do a search for each.
$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');
}
有人能帮我理解需要做些什么来纠正所述错误吗?我试过use AppPost;
,但没有用。
您需要在控制器和模型中使用命名空间
控制器:
namespace AppHttpControllers;
use AppPost;
型号:
namespace App;
名称空间允许您通过将它们定义为自己的";名称空间";。在"名称空间";关键字标识名称空间及其下面的所有代码然后住在这个名字空间里。命名空间还提供了一种方式对接口函数和常量进行分组。
您可以阅读有关命名空间的更多信息。
希望这对你的情况有效!!