简体   繁体   中英

I Want to show products items into shopping cart when i click add to cart button using laravel?

I'm trying to add products items into the shopping cart when I click to add to cart button but facing some error how to fix it error

please see this error Non-static method Gloudemans\\Shoppingcart\\Cart::add() should not be called statically http://localhost/Projects/E-commerce/cart/action

Controller

      namespace App\ Http\ Controllers;
      use Illuminate\ Http\ Request;
      use Gloudemans\ Shoppingcart\ Cart;
      class CartController extends Controller {



       public
       function cart() {
        $cartcontent = Cart::Content();
        return view('front_end/cart', ['cartcontent' => $cartcontent]);
       }

       public
       function addcart(Request $request) {
        $image = [$request - > product_image];

        $add = Cart::add(
         $request - > productid,
         $request - > product_name,
         $request - > qty,
         $request - > product_price,
         $image

        );
        if ($add) {
         redirect('/cart');

        }
       }

html view

<tbody class="cart-table__body">
                  @foreach($cartcontent as  $cartcontents)
    <tr class="cart-table__row">
        <td class="cart-table__column cart-table__column--image">
            <a href="{{$cartcontents->product_image}}">
                <img src="{{$cartcontents->product_image}}" alt="">
                </a>
            </td>
            <td class="cart-table__column cart-table__column--product">
                <a href="" class="cart-table__product-name">{{$cartcontents->product_name}}</a>
            </td>
            <td class="cart-table__column cart-table__column--price" data-title="Price"> 
                  {{$cartcontents->product_price}}</td>
            <td class="cart-table__column cart-table__column--quantity" data-title="Quantity">
                <div class="input-number">
                    <input class="form-control input-number__input" type="number" min="1" value=" 
                  {{$cartcontents->qty}}">
                        <div class="input-number__add"></div>
                        <div class="input-number__sub"></div>
                    </div>
                </td>
                <td class="cart-table__column cart-table__column--total" data-title="Total"></td>
                <td class="cart-table__column cart-table__column--remove">
                    <a href="" class="btn btn-light btn-sm btn-svg-icon">
                        <svg width="12px" height="12px">
                            <use xlink:href="{{url('public/assets/images/sprite.svg#cross-12')}}"></use>
                        </svg>
                    </a>
                </td>
            </tr>
                   @endforeach


        </tbody>

Use this method to pass cart data to your view

 public function cart()
       {
        $cartcontent = Cart::Content();
        $cart_data = [];
        foreach($cart_list as $cart){
               $cart_data [] = [

              //define keys as per your cart's content and pass $cart_data in your view
               //like this: 
              // 'product' => $cart->product

               ];
          }

        return view('front_end/cart')->with(compact(['cart_data']));

    }

After that loop through cart_data in your view just like you did.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM