mercredi 13 novembre 2019

Get product based on fixed discount percentage in laravel

I have the following code in my apps

In my database

$table->decimal('price', 12, 4);
$table->decimal('special_price', 12, 4)->nullable();
$table->date('special_price_from')->nullable();
$table->date('special_price_to')->nullable();

In my page it display like discount page

In my 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>

In my route

Route::get('/discount/products', 'DiscountController@index')->name('frontend.discount');

In my controller

public function index(Request $request) {
    $min = $request->min;
    $max = $request->max;
    $products = ProductFlat::where('special_price','>', 0)->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;
      } else {
       // $result = 0;
      }
     } elseif ($min == null && $max != null) {
       if ($discount >= $max) {
         $result = $product;
       } else {
         // $result = 0;
       }
      } elseif ($min != null && $max == null) {
        if ($discount <= $min) {
          $result = $product;
        } else {
          // $result = 0;
        }
      }
    }
    return view('discount.index', compact('result', 'min', 'max'));
}

So the question is How can I get the product when the user click a discount range and shows the discount based on the url provide like If they click 15% to 30%, product should be display will be the product which has special_price(discount price) and which is to be 15% to 30% In my case, all product displayed if they are discount or not?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire