我有一个带有分页的页面,我需要在第一页上显示价格为90 $的产品,然后显示其他价格更低或更高的产品。有什么办法吗?到目前为止,我一直尝试遍历所有产品,然后以90%的第一款价格出售产品。
foreach ($products as $key => $product) {
$real_price = $product->stock[0]->price;
if ($product->discount_type == 'discount-percentage') {
$price = $product->stock[0]->price - (($product->discount / 100) * $product->stock[0]->price);
} elseif ($product->discount_type == 'discount-amount') {
$price = $product->stock[0]->price - $product->discount;
} else {
$price = $real_price;
}
if ($price == 90) {
$products->forget($key);
$products->prepend($product);
}
}
但是有一个问题,如果价格较高的产品在下一页上,它将无法正常工作。进行此查询的另一个困难是产品还可以享受折扣,因此我必须在折扣后的价格为90美元时显示这些产品。
我可以使用Laravel雄辩的或原始的查询来做任何事情。谢谢!