JOHNMAC's avatar

Route [products.all] not defined. (\resources\views\product\cart.blade.php)

it was working fine but when i add delete cart product functionality then it started showing this error Route [products.all] not defined. (resources\views\product\cart.blade.php), the error is due to:

   @unless(empty($contents))
   @else
   <p class="alert alert-danger">No Products in the Cart <a href="{{route('products.all')}}">Buy Some Products</a></p>
   @endunless

but it only comes on deleting item from cart, any suggeston to fix it

blade

    @unless(empty($contents))

    <form action="{{ route('cart.delete', $product) }}" method="get">
                      @csrf
                    <tr class="table-row">
                        <td class="column-1">
                            <div class="cart-img-product b-rad-4 o-f-hidden">
                                <img src="{{ URL::to('/') }}/images/backend_images/product_images/{{ $cartItem['product']->product_image }}" alt="IMG-PRODUCT">
                            </div>
                        </td>
                        <td class="column-2">{{ $cartItem['product']->product_name }}</td>
                        <td class="column-3">${{ $cartItem['product']->product_price }}</td>
                        <td class="column-4">
                            <div class="flex-w bo5 of-hidden w-size17">
                                <button class="btn-num-product-down color1 flex-c-m size7 bg8 eff2">
                                    <i class="fs-12 fa fa-minus" aria-hidden="true"></i>
                                </button>

                                <input class="size8 m-text18 t-center num-product" type="number" name="num-product1" value="{{ $cartItem['qty'] }}">

                                <button class="btn-num-product-up color1 flex-c-m size7 bg8 eff2">
                                    <i class="fs-12 fa fa-plus" aria-hidden="true"></i>
                                </button>
                            </div>
                        </td>
                        <td class="column-5">${{ $cartItem['price'] }}</td>
                        <td class="column-5">
                            <input type="submit" class="btn btn-danger-sm" name="">
                        </td>
                    </tr>
                  </form>
                  @endforeach

  @else
  <p class="alert alert-danger">No Products in the Cart <a href="{{route('products.all')}}">Buy Some Products</a></p>
  @endunless

model

    public function deleteProduct($product)
   {
      if ($this->contents) {
          if (array_key_exists($product->product_slug, $this->contents)) {
              $delProduct = $this->contents[$product->slug];
              $this->totalQty -= $delProduct['qty'];
               $this->totalPrice -= $delProduct['price'];
               array_forget($this->contents, $product->slug);
         }
     }
  }

route

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

controller

    public function deleteCartProduct(Product $product)
  {
     $oldCart = Session::has('cart') ? Session::get('cart') : null;
    $cart = new Cart($oldCart);
    $cart->delProduct($product);
    Session::put('cart', $cart);

   return redirect()->route('product.cart')->with('flash_message_success', 'Product product has been removed from Cart');
  }
0 likes
3 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@incridible so the error is self descriptive..

You have this in your view

route('products.all')

so for this to work you will have to have a named route like this for example:

Route::get('/carts','ProductController@index')->name('products.all');
Nakov's avatar

@incridible now again the error says it all. You are using $product in your view, but you never pass it to the view.

When you do this:

return view('product.cart');

You will need to pass an instance of the $product in order to use it in the view:

return view('product.cart')->with('product', $product);

So this is now an extra mile from the initial question. I suggest you mark the previous answer as solution to your question, and spent at least 10 minutes on each error that you get before posting here :)

That's the way to learn.

1 like

Please or to participate in this conversation.