title: laravel session shopping cart - how to increase the quantity if the product already exists
body: <p>i'm making an online shopping project with laravel,
  But there is a problem here,
  <strong>I want if the user adds the product to the shopping cart and there is already the same product will be added quantity amount</strong>,</p>
  
  <p>but on my project: if a user adds a product to the shopping cart and already has the same product, it will add a new value item to the session</p>
  
  <p>like this :
  <a href="https://i.stack.imgur.com/LIBdc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LIBdc.png" alt="enter image description here"></a></p>
  
  <p>array :</p>
  
  <pre><code>array:3 [▼
    0 =&gt; array:5 [▼
      "id" =&gt; 7
      "nama_product" =&gt; "adssdsadxx"
      "harga" =&gt; 13
      "pict" =&gt; "s.gif"
      "qty" =&gt; 1
    ]
    1 =&gt; array:5 [▼
      "id" =&gt; 7
      "nama_product" =&gt; "adssdsadxx"
      "harga" =&gt; 13
      "pict" =&gt; "s.gif"
      "qty" =&gt; 1
    ]
    2 =&gt; array:5 [▼
      "id" =&gt; 7
      "nama_product" =&gt; "adssdsadxx"
      "harga" =&gt; 13
      "pict" =&gt; "s.gif"
      "qty" =&gt; 1
    ]
  ]
  </code></pre>
  
  <p>I want it like this (if the user adds the product to the shopping cart and there is already the same product will be added quantity amount):
  <a href="https://i.stack.imgur.com/AsuRm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AsuRm.png" alt="enter image description here"></a></p>
  
  <p>array :</p>
  
  <pre><code>array:1 [▼
    0 =&gt; array:5 [▼
      "id" =&gt; 7
      "nama_product" =&gt; "adssdsadxx"
      "harga" =&gt; 39
      "pict" =&gt; "s.gif"
      "qty" =&gt; "3"
    ]
  ]
  </code></pre>
  
  <p>this is my <em>ProductsController.php</em> :</p>
  
  <pre><code>   public function Cart()
      {
          return view('shop.cart');
      }
  
      public function addToCart(Request $request, $id)
      {
          $product = DB::select('select * from products where id='.$id);
          $cart = Session::get('cart');
          $cart[] = array(
              "id" =&gt; $product[0]-&gt;id,
              "nama_product" =&gt; $product[0]-&gt;nama_product,
              "harga" =&gt; $product[0]-&gt;harga,
              "pict" =&gt; $product[0]-&gt;pict,
              "qty" =&gt; 1,
          );
  
          Session::put('cart', $cart);
          Session::flash('success','barang berhasil ditambah ke keranjang!');
          //dd(Session::get('cart'));
          return redirect()-&gt;back();
      }
  
      public function updateCart(Request $cartdata)
      {
          $cart = Session::get('cart');
  
          $cartQty = 1;
  
          foreach ($cartdata-&gt;all() as $id =&gt; $val) 
          {
              if ($cartQty != 1) {
                  $cart[$id]['qty'] = $val;
                  if ($val &lt; 1) {
                      unset($cart[$id]);
                  }
              }
              $cartQty++;
          }
          Session::put('cart', $cart);
          return redirect()-&gt;back();
      }
  
      public function deleteCart($id)
      {
          $cart = Session::get('cart');
          unset($cart[$id]);
          Session::put('cart', $cart);
          return redirect()-&gt;back();
      }
  </code></pre>
  
  <p>Thank you very much...</p>