matildah
5 months ago
740
4
Laravel

Button not redirecting

Posted 5 months ago by matildah

i have an order button that is supposed to redirect the user to the cart page with the items ordered

Order this

This is the route on web.php

Route::get('add-to-cart/{id}', '[email protected]')->name('addCart');

This is the function addToCart

public function addToCart($id){ $food = Food::find($id);

    if(!$food) {

        abort(404);

    }

    $cart = session()->get('cart');

    // if cart is empty then this the first product
    if(!$cart) {

        $cart = [
                $id => [
                    // "productId" => $food->id,
                    "name" => $food->food_item,
                    "quantity" => 1,
                    "price" => $food->price,

                ]
        ];


        session()->put('cart', $cart);


        return redirect()->back()->with('success', 'Product added to cart successfully!');
    }

    // if cart not empty then check if this product exist then increment quantity
    if(isset($cart[$id])) {

        $cart[$id]['quantity']++;

        session()->put('cart', $cart);

        return redirect()->back()->with('success', 'Product added to cart successfully!');

    }

    // if item not exist in cart then add to cart with quantity = 1
    $cart[$id] = [
        // "productId" => $food->id,
        "name" => $food->food_item,
        "quantity" => 1,
        "price" => $food->price,

    ];


    session()->put('cart', $cart);

    return redirect()->back()->with('success', 'Product added to cart successfully!');
}

But when i click the button it doesn't redirect to the cart page it keeps loading to the same page

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