【发布时间】:2020-08-28 20:20:45
【问题描述】:
问题:
我有一个名为product 的表,其中包含一些列。其中之一是name。问题是:
示例条目:
- 名称:盐粉。
- 名称:辣椒粉R
问题
当我查询 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');
}
我的productRepository.php:
<?php
namespace App\Repositories;
use App\Models\Product;
use InfyOm\Generator\Common\BaseRepository;
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Traits\CacheableRepository;
class ProductRepository extends BaseRepository implements CacheableInterface
{
use CacheableRepository;
/**
* @var array
*/
protected $fieldSearchable = [
'name',
'seokeywords',
'price',
'discount_price',
'description',
'capacity',
'package_items_count',
'unit',
'itemsAvailable',
'featured',
'store_id',
'category_id',
'brand_id'
];
/**
* Configure the Model
**/
public function model()
{
return Product::class;
}
/**
* get my products
**/
public function myProducts()
{
return Product::join("user_stores", "user_stores.store_id", "=", "products.store_id")
->where('user_stores.user_id', auth()->id())->get();
}
}
有人可以帮助理解我应该修改什么或哪个语句吗?我已经尝试过更改,但大多数尝试都以错误告终。任何帮助深表感谢。我可以分享你们可能感兴趣的任何文件
【问题讨论】:
-
我猜你需要将你的通配符搜索子句更正为
where('name', 'LIKE', '%'.$queryString.'%') -
但是 if (Query::has('search')) { $queryString = Query::get('search') 行出现语法错误。查看上面的索引,应该添加什么而不是“查询”,我可以从中检查术语关键字是否存在。你能帮忙吗?
-
是“name:”部分存储在db中的数据吗?
-
@Rabah:是的,先生,是的。具体来说,它是“产品”表中的一列
-
对不起,我不清楚:存储在“名称”列中的数据是:“盐粉”还是:“名称:盐粉”
标签: php laravel laravel-5 search