我想归档产品列表并对其进行排序。我有2个产品数据表。
Products -> [id, name, description, point, data, etc.]
在此表中,数据是一个json对象。该对象包含产品的所有规格。 Ram,相机,存储设备等。我也想使用这些规格过滤数据。以ram为例进行过滤。
Prices -> [id, porduct_id, link, price]
一个产品可以有多个价格,但我只需要使用最低价格进行过滤。而且,如果我要对其进行排序,则将考虑最低价格。
我已经实现了过滤器代码。但是我被困在实现过滤器之后的排序。按价格排序的价格范围过滤器对我来说似乎很困难。 有人可以帮我吗?
价格范围过滤器的当前代码
if (isset($filters['price'])) {
$product = $product->whereHas('prices', function ($query) use ($filters) {
$query->whereBetween('price', [$filters['price']['min'], $filters['price']['max']]);
});
}
其他数据过滤器的当前代码:
if ($request->has('filters') && !empty($filters = $request->get('filters'))) {
$product = $product->filter($request->get('filters'));
}
当前按价格排序的代码
if ($request->has('price')) {
$direction = in_array($request->query('price'), ['asc', 'desc']) ? $request->query('price') : 'asc';
return ProductResource::collection(
$product
->addSelect(['min_price' => Price::select('price')
->whereColumn('product_id', 'products.id')
->oldest('price')
->take(1)
])
->orderBy('min_price', $direction)
->paginate(self::PAGINATE_NUMBER)
);
}
我坚持同时执行这两项操作。 如果您需要有关此问题的更多信息,请发表评论。