Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Product Quantity reduce for every sale

I have an Laravel store with Product Quantity in my DB.

The Quantity left what Members see :

Quantity : {{$product->instock}}

Product Create :

<input type="number" name="instock" id="instock" class="productcreate" value="{{old('instock')}}" placeholder="Enter Your Quantity"> in Stock

Controller:

  if ($request->instock == null) {
    session()->flash('errormessage','Quantity is required');
    return redirect()->back()->withInput();
  }

How can I reduce my Quantity for every Sold item? Example I have 90 Items right now in stock. An Member buy one Item and the Quantity Reduce to 89 Items right now in Quantity: {{$product->instock}}

What code must I add in my Controller for it?

Thanks

like image 599
sarahconor Avatar asked Nov 25 '25 12:11

sarahconor


1 Answers

When someone makes a purchase use the decrement() method. Something like:

Product::decrement('instock', $itemsPurchased);
like image 175
Alexey Mezenin Avatar answered Nov 27 '25 01:11

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!