You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel添加购物车删除功能后出现Route [products.all]未定义错误

Hey there, let's break down the issues you're facing one by one and fix them step by step:

1. Fix the "Route [products.all] not defined" Error

This error comes straight from the empty cart message in your view:

<a href="{{route('products.all')}}">Buy Some Products</a>

You haven't set up a route with the name products.all in your routes file. To fix this, you have two options:

  • Add a named route for your product listing page (make sure your ProductController has an index method that returns the product list view):
    // In your routes file
    Route::get('/products', 'ProductController@index')->name('products.all');
    
  • Or replace the route helper with the direct URL if you don't want to name the route:
    <a href="/products">Buy Some Products</a>
    

2. Fix the Mismatched Delete Route

Your delete form is using route('cart.delete', $product) but your route is named deleteCartProduct—this will trigger another route-not-found error once you fix the first issue. Update your form action to match the route name you defined:

<form action="{{ route('deleteCartProduct', $product->id) }}" method="POST">

Also, since this is a delete operation, it's better practice to use POST (or DELETE with method spoofing) instead of GET. Double-check that the @csrf directive is inside the form tag (your current code has this right, but it's easy to misplace!).

3. Fix the Product Slug Mismatch in Cart Model

In your Cart model's deleteProduct method, you're referencing $product->slug, but from your view code, the product's slug field is actually product_slug. This means the cart won't find the product to delete. Update the model code:

public function deleteProduct($product) {
    if ($this->contents) {
        // Use the correct field name: product_slug
        $productSlug = $product->product_slug;
        if (array_key_exists($productSlug, $this->contents)) {
            $delProduct = $this->contents[$productSlug];
            $this->totalQty -= $delProduct['qty'];
            $this->totalPrice -= $delProduct['price'];
            array_forget($this->contents, $productSlug);
        }
    }
}

4. Fix Invalid HTML Structure

Your form is wrapping a <tr> element, which is invalid HTML—<form> can't be a direct parent of table rows. Adjust the structure by moving the form inside the last table cell that holds the delete button:

<td class="column-5">
    <form action="{{ route('deleteCartProduct', $product->id) }}" method="POST">
        @csrf
        <input type="submit" class="btn btn-danger-sm" value="Remove">
    </form>
</td>

If you prefer a cleaner link-style button, you can use this approach (with RESTful DELETE method):

<td class="column-5">
    <a href="#" 
       class="btn btn-danger-sm"
       onclick="event.preventDefault(); document.getElementById('delete-form-{{ $product->id }}').submit();">
        Remove
    </a>
    <form id="delete-form-{{ $product->id }}" action="{{ route('deleteCartProduct', $product->id) }}" method="POST" style="display: none;">
        @csrf
        @method('DELETE')
    </form>
</td>

And update your route to accept DELETE requests:

Route::delete('/cart/delete-product/{id}','ProductController@deleteCartProduct')->name('deleteCartProduct');

After making these changes, both the route error and the cart delete functionality should work as expected.

内容的提问来源于stack exchange,提问作者binadam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:20:30