当我单击“添加到购物车”按钮时,我试图将产品添加到购物车中,但是遇到一些错误,如何解决它
请参阅此错误 非静态方法Gloudemans \ Shoppingcart \ Cart :: add()不应静态调用 http://localhost/Projects/E-commerce/cart/action
控制器
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视图
<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>
答案 0 :(得分:1)
使用此方法将购物车数据传递到您的视图
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']));
}
之后,就像您一样在您的视图中循环遍历cart_data。