Php 要使用laravel将产品项目显示到购物车中吗

Php 要使用laravel将产品项目显示到购物车中吗,php,laravel,Php,Laravel,我正在尝试在购物车上显示商品,但遇到一些错误,如何修复它? 不应使用非静态方法gludemans\Shoppingcart\Cart::add() 静态调用 控制器 public function cart() { $cart_list = Cart::Content(); $cart_data = []; foreach($cart_list as $cart){ $cart_data [] = [ 'product_name' => $cart-

我正在尝试在购物车上显示商品,但遇到一些错误,如何修复它?

不应使用非静态方法gludemans\Shoppingcart\Cart::add() 静态调用

控制器

public function cart()
{
   $cart_list = Cart::Content();
   $cart_data = [];
    foreach($cart_list as $cart){
     $cart_data [] = [
     'product_name' => $cart->product_name,
     'product_image' => $cart->product_image,
     'qty' => $cart->qty,
     'product_price' => $cart->product_price,
     ];
   }
     return view('front_end/cart')->with(compact(['cart_data']));
 }


public function addcart(Request $request){
     $productId=$request->id;
     $productById = DB::table('products')->where('id', $productId)->first(); // Get all 
       Cart::add([
                 'id'=>  $productId,
                 'product_name'=>$productById->product_name,
                 'product_price'=>$productById->product_price,
                 'qty'=>$request->qty,
                 'product_image'=>$productById->product_image,
                ]);
   return redirect('/cart');

使用
创建
方法

  Cart::create([
                 'id'=>  $productId,
                 'product_name'=>$productById->product_name,
                 'product_price'=>$productById->product_price,
                 'qty'=>$request->qty,
                 'product_image'=>$productById->product_image,
                ]);
   return redirect('/cart');

使用
创建
方法

  Cart::create([
                 'id'=>  $productId,
                 'product_name'=>$productById->product_name,
                 'product_price'=>$productById->product_price,
                 'qty'=>$request->qty,
                 'product_image'=>$productById->product_image,
                ]);
   return redirect('/cart');
在控制器中:

public function cart()
{
    return view('frontend/cart');
}

public function addCart(Request $request)
{
    $product = Product::findOrFail($request->id);

    $cartItem = Cart::add([
         'id' => $product->id,
         'name' => $product->name,
         'qty' => $request->qty,
         'price' => $product->price,
     ]);

     Cart::associate($cartItem->rowId, 'App\Product');

     return redirect()->route('cart');
}
使用associate()方法,可以告诉购物车其中的一个项目与产品模型关联

在您看来,您可以这样做:

@foreach (Cart::content() as $item)
    // your code here
    // but try to access your values as $item->rowId, $item->name, $item->qty, $item->price

@endforeach
<img src="{{ asset($item->model->image) }}" alt="product">
然后,对于产品图像,您可以执行以下操作:

@foreach (Cart::content() as $item)
    // your code here
    // but try to access your values as $item->rowId, $item->name, $item->qty, $item->price

@endforeach
<img src="{{ asset($item->model->image) }}" alt="product">
model->image)}“alt=”product“>
在控制器中:

public function cart()
{
    return view('frontend/cart');
}

public function addCart(Request $request)
{
    $product = Product::findOrFail($request->id);

    $cartItem = Cart::add([
         'id' => $product->id,
         'name' => $product->name,
         'qty' => $request->qty,
         'price' => $product->price,
     ]);

     Cart::associate($cartItem->rowId, 'App\Product');

     return redirect()->route('cart');
}
使用associate()方法,可以告诉购物车其中的一个项目与产品模型关联

在您看来,您可以这样做:

@foreach (Cart::content() as $item)
    // your code here
    // but try to access your values as $item->rowId, $item->name, $item->qty, $item->price

@endforeach
<img src="{{ asset($item->model->image) }}" alt="product">
然后,对于产品图像,您可以执行以下操作:

@foreach (Cart::content() as $item)
    // your code here
    // but try to access your values as $item->rowId, $item->name, $item->qty, $item->price

@endforeach
<img src="{{ asset($item->model->image) }}" alt="product">
model->image)}“alt=”product“>


购物车是模态库或另一个库…你在哪里定义变量
$Cart
?你能在导入
购物车
的地方添加代码吗?购物车是模态库或另一个库…你在哪里定义变量
$Cart
?你能在导入
购物车
的地方添加代码吗?希望它能工作!!!!请选择这个答案,如果它对你有帮助,谢谢你希望它的工作!!!!如果对您有帮助,请选择此答案谢谢我希望您觉得此答案有帮助!请记住,您根据需要使用钥匙的名称,即
id
name
qty
、以及
price
,如何修复此错误?调用未定义的函数gludemans\Shoppingcart\array\u get()似乎您正在使用Laravel 6,而购物车软件包还不支持它。我建议您按照官方文档中的说明进行操作。或者您可以通过在
供应商中查找包来直接对包进行一些调整。在
src/CartItem.php
中,您可以将
array\u get()
的所有实例替换为
\Arr::get()
,希望这对您有所帮助!请记住,您根据需要使用钥匙的名称,即
id
name
qty
、以及
price
,如何修复此错误?调用未定义的函数gludemans\Shoppingcart\array\u get()似乎您正在使用Laravel 6,而购物车软件包还不支持它。我建议您按照官方文档中的说明进行操作。或者您可以通过在
供应商中查找包来直接对包进行一些调整。在
src/CartItem.php
中,可以用
\Arr::get()
替换
数组的所有实例