Php 根据laravel中的固定折扣百分比获取产品

Php 根据laravel中的固定折扣百分比获取产品,php,laravel-5,Php,Laravel 5,我的应用程序中有以下代码 在我的数据库中 $table->decimal('price', 12, 4); $table->decimal('special_price', 12, 4)->nullable(); $table->date('special_price_from')->nullable(); $table->date('special_price_to')->nullable(); 在我的页面中,它显示为 在我的html中 <a

我的应用程序中有以下代码

在我的数据库中

$table->decimal('price', 12, 4);
$table->decimal('special_price', 12, 4)->nullable();
$table->date('special_price_from')->nullable();
$table->date('special_price_to')->nullable();
在我的页面中,它显示为

在我的html中

<a href="/discount/products?min=15&max=30">
  <div class="spacer"></div>
  <div class="outer-container bg-orange">
   <div class="inner-container">
    <small class="font-weight-bold small-text-2">below</small>
    <h3 class="font-weight-bold small-text text-white">15%</h3>
   </div>
  </div>
 </a>
在我的控制器中

public function index(Request $request) {
    $min = $request->min;
    $max = $request->max;
    $products = ProductFlat::where('special_price','>', 0)->where('special_price_from', '>=', date('Y-m-d')->where('special_price_to', '<=', date('Y-m-d')->get();
    foreach ($products as $product) {
     $discount = (($product->price - $product->special_price) * 100) / $product->price;
     if ($min != null && $max != null) {
      if ($discount <= $max && $discount >= $min) {
       $result = $product->with('product')->get();
      } else {
       // $result = 0;
      }
     } elseif ($min == null && $max != null) {
       if ($discount >= $max) {
         $result = $product->with('product')->get();
       } else {
         // $result = 0;
       }
      } elseif ($min != null && $max == null) {
        if ($discount <= $min) {
          $result = $product->with('product')->get();
        } else {
          // $result = 0;
        }
      }
    }
    return view('discount.index', compact('result', 'min', 'max'));
}
公共功能索引(请求$Request){
$min=$request->min;
$max=$request->max;

$products=ProductFlat::where('special_price','>',0)->where('special_price_from','>=',date('Y-m-d')->where('special_price_to','p>)为每个正确找到的产品覆盖
$result

替换这个

$result = $product->with('product')->get();
为此:

$result[] = $product->with('product')->get();
您还可以收集
product\u id
并使用
其中
进行单个查询,而不是查询每个产品的数据

$result[] = $product->with('product')->get();