AndreLan
119
24
Laravel

Can't delete items from cart, only quantity and leaving the item with 0 quantity

Posted 4 months ago by AndreLan

When I delete from the item's quantity it works fine, I just click on delete then a pop-up input form will appear then I write the quantity I want to delete then submit.. for example, I have a watch in my cart with quantity=20, when I want to delete 5 of them I have to write 5 then delete and after that I will have the same item of course with quantity=15, but when I write 15 to delete the whole order it only deletes the quantity and leaves the item with quantity=0, also if the quntity=1 I want to delete the item permanently when clicking delete, I do not need the pop-up input form to appear.. how to do that. I tried both of the commented methods but with no result, only errors.

this is my destroy function in OrderController

public function destroy(Request $request, $id)
        {
        $validator = Validator::make($request->all(), [
            'qty' => 'required|max:255',
            
        ]);
        $order = Order::find($id);
        if ($request->input('qty') > $order->qty) {
            return redirect('/my_cart')
                        ->withErrors($validator)
                        ->withInput();
        }


        // if ($request->input('qty') = $order->qty || $request->input('qty') = 1) {
        //     return redirect('/my_cart')
        //                 ->delete();
        // }

        // $order = Order::where('qty','=',$request->input('qty'))->delete();        
        

        $product= Product::find($order->product_id);
        $order->qty = $order->qty - $request['qty'];
        $product->inv = $product->inv + $request->input('qty');
        
        $product->save();
        $order->save();
        
        return redirect('/my_cart');

    }

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.