Crinsane / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
3.67k stars 1.73k forks source link

check quantity #497

Open potskhveria opened 6 years ago

potskhveria commented 6 years ago

Can I check quantity of item on add or upddate cart?

potskhveria commented 6 years ago

During the update, I check the number, but I can not check when I add. How do I check the amount when an item is not added to the trash?

Update check

if($request->qty > $cartItem->model->qty ) {

Cart::update($cartItem->rowId, ['qty' => $cartItem->model->qty, 'options' => ['size' => $request->size ]] );

return redirect('cart')->with('alert_fail', "Updated to the maximum number"); } elseif ($cartItem->model->qty >= $request->qty) { Cart::update($cartItem->rowId, ['qty' => $request->qty, 'options' => ['size' => $request->size ]] ); return redirect('cart')->with('alert_ok', "Cart successfully updated"); }

GordanaP commented 6 years ago

I'm afraid your approach allows only a decrease in the qty, not an increase. I would suggest that you use the package update cart functionality:

Cart::update($rowId, $request->qty);

and limit the number of items allowed in the cart using Laravel validation, i.e.


'quantity' => 'required|min:1|max:10'