I want to filer product list and sort it too. I have 2 tables for product data.
Products -> [id, name, description, point, data, etc.]
in this table, data is a json object. This object contains all the specifications of a product. Ram, Camera, storage etc. I want to filter data using these specs too. Filter using ram for example.
Prices -> [id, porduct_id, link, price]
one product can have multiple prices but i need to filter using only minimum price. And if I want to sort it, minimum price will be considered for sorting.
I have implemented code for filter. But I'm stuck at implementing sort after filter. Price range filter with sorting by price seems difficult for me. can anyone help me out?
Current code for price range filter
if (isset($filters['price'])) {
$product = $product->whereHas('prices', function ($query) use ($filters) {
$query->whereBetween('price', [$filters['price']['min'], $filters['price']['max']]);
});
}
Current code for other data filters:
if ($request->has('filters') && !empty($filters = $request->get('filters'))) {
$product = $product->filter($request->get('filters'));
}
Current code for sorting by price
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)
);
}
I'm stuck at doing both of these operation together. If you need more info about this question, please comment.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire