【问题标题】:How to show items into shopping cart when click add to cart button in laravel?单击laravel中的添加到购物车按钮时如何将商品显示到购物车中?
【发布时间】:2020-03-23 19:09:03
【问题描述】:

当我点击添加到卡片按钮时,我正在尝试将商品显示到购物车中,但我遇到了一些错误如何适合它错误

请查看错误 不应静态调用非静态方法 Gloudemans\Shoppingcart\Cart::add()

https://flareapp.io/share/Bm06697x

控制器

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use Gloudemans\Shoppingcart\Cart;

   class CartController extends Controller
   {
   public function cart()
   { 
   return view('front_end/cart');
   }

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

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

     );
    return redirect()->route('cart.index')->with('success_message', 'Item was added to your cart!');
     }
    }

html 视图

              <tbody class="cart-table__body">

              @foreach(Cart::content()  as $items)
              <tr class="cart-table__row">
              <td class="cart-table__column cart-table__column--image">
              <a href=""><img src="{{$items->product_image}}" alt=""></a>
              </td>
              <td class="cart-table__column cart-table__column--product">
              <a href="" class="cart-table__product-name">{{$items->product_name}}</a>        
              </td>
             <td class="cart-table__column cart-table__column--price" data-title="Price">{{$items- 
              >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="{{$items->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>

【问题讨论】:

  • Cart::content() 你不应该在刀片中使用它。创建一个函数并返回一个集合或数组并在刀片中使用它可以防止上述错误

标签: html laravel


【解决方案1】:

在您的控制器中使用它并在您的视图中传递数据

        $cart_list = 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
               ];
        }


    }

【讨论】:

    【解决方案2】:

    编辑: 对不起,我读错了问题。 在哪里定义你的函数 add() 只需添加静态

    public static function add() {
    //.....
    }
    

    static public function add() {
        // ......
    }
    

    旧帖 我没有看到,你在哪里定义哪个购物车是为哪个用户定义的,但我会这样做。

    控制器

    $userId = Auth::id(); // Get the ID from Session of a logged user.
    
    $carts = Cart::where('user_id', $userId)->get(); // Get all products linked to user
    
    return view('cart')->withCarts($carts); // Return the view with data
    

    刀片

    @foreach($carts as $cart) // U foreach every product linked to user
    
    {{ $cart->product_name }}
    {{ $cart->product_price}}
    {{ $cart->product_image }}
    
    @endforeach
    

    如果用户未登录,您可以使用 cookie id 创建购物车 youtube 上有数百个教程如何制作电子商务购物车。

    希望对你有帮助

    【讨论】:

    • 我使用了静态关键字,但我有同样的错误非静态方法 Gloudemans\Shoppingcart\Cart::add() 不应该被静态调用flareapp.io/share/w5BGAXm8#F43
    • 如果你尝试将 use Gloudemans\Shoppingcart\Cart; 更改为 use Cart;
    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2017-02-07
    • 1970-01-01
    相关资源
    最近更新 更多