Search code examples
laravelhttp-status-code-404shopping-cart

404 on add to cart laravel


i have this situation on add to cart when i want to add to cart i am getting 404 error,

this is my route

Route::get('/add-to-cart/{product}', [CartController::class, 'addToCart'])->name('add.cart');
Route::get('/remove/{id}', [CartController::class, 'removeFromCart'])->name('remove.cart');
Route::get('/change-qty/{product}', [CartController::class, 'changeQty'])->name('change_qty');

This is controller namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Products;

class CartController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    //
    return view('cart.index');
}

public function addToCart(Products $product)
{
    $cart = session()->get('cart');
    if (!$cart) {
        $cart = [$product->id => $this->sessionData($product)];
        return $this->setSessionAndReturnResponse($cart);
    }
    if (isset($cart[$product->id])) {
        $cart[$product->id]['quantity']++;
        return $this->setSessionAndReturnResponse($cart);
    }
    $cart[$product->id] = $this->sessionData($product);
    return $this->setSessionAndReturnResponse($cart);

}

public function changeQty(Request $request, Products $product)
{
    $cart = session()->get('cart');
    if ($request->change_to === 'down') {
        if (isset($cart[$product->id])) {
            if ($cart[$product->id]['quantity'] > 1) {
                $cart[$product->id]['quantity']--;
                return $this->setSessionAndReturnResponse($cart);
            } else {
                return $this->removeFromCart($product->id);
            }
        }
    } else {
        if (isset($cart[$product->id])) {
            $cart[$product->id]['quantity']++;
            return $this->setSessionAndReturnResponse($cart);
        }
    }

    return back();
}

public function removeFromCart($id)
{
    $cart = session()->get('cart');

    if (isset($cart[$id])) {
        unset($cart[$id]);
        session()->put('cart', $cart);
    }
    return redirect()->back()->with('success', "Removed from Cart");
}

protected function sessionData(Products $product)
{
    return [
        'name' => $product->nume,
        'quantity' => 1,
        'price' => $product->pret,
    ];
}

protected function setSessionAndReturnResponse($cart)
{
    session()->put('cart', $cart);
    return redirect()->route('cart')->with('success', "Added to Cart");
}

This is what i have in view file:

<a class="theme_btn add_cart w-100" href="{{route('add.cart', [$produs->id])}}">add to cart
     <span class="theme_btn_eff"></span>
</a>

In my loop when i fetch products, on click is redirecting me to 404 page without getting an error, i was trying to get dd($product) into controller but i get again 404.


Solution

  • Change this line

    <a class="theme_btn add_cart w-100"
    href="{{ route('add.cart', [$produs->id]) }}" >add to cart
    <span class="theme_btn_eff"></span>
    </a>
    

    To This

     <a class="theme_btn add_cart w-100"
    href="{{ route('add.cart', ['product' => $produs->id]) }}" >add to cart
    <span class="theme_btn_eff"></span>
    </a>
    

    In routes file you have passed route parameter naming {product} in url /add-to-cart/{product}, hence you need to mention it in view file as well {{ route('add.cart', ['product' => $produs->id]) }} thats the naming convention laravel follows

    Route::get('/add-to-cart/{product}', [CartController::class, 'addToCart'])->name('add.cart');