title: how to reduce product stock in laravel?
body: <p>I just learned about laravel framework and I have a project to make ecommerce. But there are constraints when reducing product stocks, is there anything that can help me when customers add quantity in product detail and renew shopping cart then stock of product reduce.</p>
  
  <p>Actually the problem I do not know how to stock the product is reduced on the database, when the customer adds quantity in the shopping cart. Example product a has stock 12 on the database, when customer adds quantity in cart as much 12 after checkout stage is finished then stock of product residing in database runs out.</p>
  
  <p>App/Http/Controllers/CartController.php</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Product;
  use Session;
  use App\Support\CartService;
  use Laracasts\Flash\Flash;
  use Auth;
  use App\Cart;
  use Illuminate\Http\Request;
  
  class CartController extends Controller
  {
  
      public function addProduct(Request $request)
      {
          $this-&gt;validate($request, [
              'product_id' =&gt; 'required|exists:products,product_id',
              'quantity' =&gt; 'required|integer|min:1',
              'size' =&gt; 'required'
          ]);
  
          $product = Product::find($request-&gt;get('product_id'));
          $quantity = $request-&gt;get('quantity');
          $size = $request-&gt;get('size');
          Session::flash('flash_product_name', $product-&gt;name);
  
          if(Auth::check()) {
              $cart = Cart::firstOrCreate([
                  'product_id' =&gt; $product-&gt;product_id,
                  'user_id' =&gt; $request-&gt;user()-&gt;user_id
              ]);
  
              $cart-&gt;quantity += $quantity;
              $cart-&gt;size = $size;
              $cart-&gt;save();
              return redirect()-&gt;route('index');
          } else {
              $cart = $request-&gt;cookie('cart', []);
              if(array_key_exists($product-&gt;product_id, $cart)) {
                  $quantity += $cart[$product-&gt;product_id];
              }
              $cart[$product-&gt;product_id] = $quantity;
              return redirect()-&gt;route('index')-&gt;withCookie(cookie()-&gt;forever('cart', $cart));
          }
      }
  
      public function changeQuantity(Request $request, $product_id)
      {
          $this-&gt;validate($request, [
              'quantity' =&gt; 'required|integer|min:1',
              'size' =&gt; 'required'
          ]);
  
          $quantity = $request-&gt;get('quantity');
          $size = $request-&gt;get('size');
          $cart = $this-&gt;cart-&gt;find($product_id);
          if(!$cart) return redirect('cart');
  
          Flash::success('Keranjang untuk produk ' . $cart['detail']['name'] . ' berhasil diubah');
          if(Auth::check()) {
              $cart = Cart::firstOrCreate([
                  'user_id' =&gt; $request-&gt;user()-&gt;user_id,
                  'product_id' =&gt; $product_id
              ]);
  
              $cart-&gt;quantity = $quantity;
              $cart-&gt;size = $size;
              $cart-&gt;save();
              return redirect('cart');
          } else {
              $cart = $request-&gt;cookie('cart', []);
              $cart[$product_id] = $quantity;
              return redirect('cart')-&gt;withCookie(cookie()-&gt;forever('cart', $cart));
          }
      }
  
  
  }
  </code></pre>
  
  <p>App/Support/CartService.php</p>
  
  <pre><code>&lt;?php
  
  namespace App\Support;
  
  use App\Product;
  use Auth;
  use App\Cart;
  use Cookie;
  use App\Address;
  use Illuminate\Http\Request;
  
  class CartService
  {
    public function lists()
    {
        if(Auth::check()) {
            return Cart::where('user_id', Auth::user()-&gt;user_id)-&gt;lists('quantity', 'product_id');
        } else {
            return $this-&gt;request-&gt;cookie('cart');
        }
    }
  
    public function totalProduct()
    {
        return count($this-&gt;lists());
    }
  
    public function isEmpty()
    {
        return $this-&gt;totalProduct() &lt; 1;
    }
  
    public function totalQuantity()
    {
        $total = 0;
        if ($this-&gt;totalProduct() &gt; 0)
        {
          foreach ($this-&gt;lists() as $id =&gt; $quantity) {
            $product = Product::find($id);
            $total += $quantity;
          }
        }
        return $total;
    }
  
    public function details()
    {
        $result = [];
        if ($this-&gt;totalProduct() &gt; 0) {
            foreach ($this-&gt;lists() as $id =&gt; $quantity) {
                $product = Product::find($id);
                array_push($result, [
                    'product_id' =&gt; $id,
                    'detail' =&gt; $product-&gt;toArray(),
                    'quantity' =&gt; $quantity,
                    'subtotal' =&gt; $product-&gt;price * $quantity
                ]);
            }
        }
        return $result;
    }
  
    public function totalPrice()
    {
        $result = 0;
        foreach ($this-&gt;details() as $order) {
        $result += $order['subtotal'];
        }
        return $result;
    }
  
    public function find($product_id)
    {
        foreach ($this-&gt;details() as $order) {
        if ($order['product_id'] == $product_id) return $order;
        }
        return null;
    }
  
    public function merge()
    {
        $cart_cookie = $this-&gt;request-&gt;cookie('cart', []);
        foreach($cart_cookie as $product_id =&gt; $quantity) {
            $cart = Cart::firstOrCreate([
                'user_id' =&gt; $this-&gt;request-&gt;user()-&gt;user_id,
                'product_id' =&gt; $product_id
            ]);
            $cart-&gt;quantity = $cart-&gt;quantity &gt; 0 ? $cart-&gt;quantity : $quantity;
            $cart-&gt;save();
        }
        return Cookie::forget('cart');
    }
  
    protected function getDestinationId()
    {
        if (Auth::check() &amp;&amp; session()-&gt;has('checkout.address.address_id')) {
            $address = Address::find(session('checkout.address.address_id'));
            return $address-&gt;regency_id;
        }
        return session('checkout.address.regency_id');
    }
  
    public function shippingFee()
    {
        $totalFee = 0;
        foreach ($this-&gt;lists() as $id =&gt; $quantity) {
            $fee = Product::find($id)-&gt;getCostTo($this-&gt;getDestinationId()) * $quantity;
            $totalFee += $fee;
        }
        return $totalFee;
    }
  
  }
  </code></pre>