当我单击“添加到卡”按钮时,我试图将产品显示到购物车中,如何修复该错误?调用未定义的函数Gloudemans \ Shoppingcart \ array_get()
https://flareapp.io/share/B5ZeYg7o#F46
控制器
public function cart()
{
return view('frontend/cart');
}
public function addCart(Request $request)
{
$product = Product::findOrFail($request->id);
$cartItem = Cart::add([
'id' => $product->id,
'product_name' => $product->name,
'product_brand'=>$product->product_brand,
'qty' => $request->qty,
'product_price' => $product->product_price,
]);
Cart::associate($cartItem->rowId, 'App\Product');
return redirect()->route('cart.index');
}
}
模型产品
class product extends Model
{
protected $fillable =[ 'id', 'product_name', 'product_price',
'product_image',
'product_brand'];
}
html视图
<tbody class="cart-table__body">
@foreach (Cart::content() as $item)
<tr class="cart-table__row">
<td class="cart-table__column cart-table__column--image">
<a href=""><img src="{{ asset($item->model->image) }}" alt="product">
</a>
</td>
<td class="cart-table__column cart-table__column--product">
<a href="" class="cart-table__product-name">{{$item->model->product_name}}</a>
</td>
<td class="cart-table__column cart-table__column--price" data-title="Price">
{{$item->model->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="
{{$item->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>
表单html视图
<form action="{{route('cart.action')}}" method="post"
class="product__options">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{$single_products->id}}">
<input type="hidden" name="product_name" value="
{{$single_products->product_name}}">
<input type="hidden" name="product_image" value="
{{$single_products-
>product_image}}">
<input type="hidden" name="product_brand" value="
{{$single_products->product_brand}}">
<input type="hidden" name="product_price" value="
{{$single_products->product_price}}">
<div class="form-group product__option">
<label class="product__option-label" for="product-
quantity">Quantity</label>
<div class="product__actions">
<div class="product__actions-item">
<div class="input-number product__quantity">
<input id="product-quantity" name="qty" class="input-
number__input form-control
form-control-lg" type="number" min="1" value="1">
<div class="input-number__add"></div>
<div class="input-number__sub"></div>
</div>
</div>
<div class="product__actions-item product__actions-item--
addtocart">
<button class="btn btn-primary btn-lg">Add to
cart</button>
</div>
<div class="product__actions-item product__actions-item--
wishlist">
<button type="button" class="btn btn-secondary btn-svg-
icon btn-lg" data-
toggle="tooltip" title="Wishlist">
<svg width="16px" height="16px">
<use xlink:href="
{{url('public/assets/images/sprite.svg#wishlist-
16')}}"></use>
</svg>
</button>
</div>
<div class="product__actions-item product__actions-item--
compare">
<button type="button" class="btn btn-secondary btn-svg-
icon btn-lg" data-
toggle="tooltip" title="Compare">
<svg width="16px" height="16px">
<use xlink:href="
{{url('public/assets/images/sprite.svg#compare-16')}}">
</use>
</svg>
</button>
</div>
</div>
</div>
</form>
Route::get('/cart','CartController@cart')->name('cart.index');
Route :: post('购物车/操作','CartController @ addcart')-
name('cart.action');
答案 0 :(得分:1)
Laravel在5.8 / 6.0左右引入了重大更改,其中他们删除了许多辅助功能,而倾向于使用外墙。
问题在于该功能:
arr_get()
导致错误的不幸地不再是标准Laravel构建的一部分。如果是在您自己的班级中,则可以将其更改为使用立面:
use Illuminate\Support\Arr;
,然后在您的方法中:
Arr::get();
您可以检查是否具有最新版本的购物车程序包,而不需要更改供应商代码,该版本可以与Laravel的更高版本兼容...或者您可以添加the helper package from Laravel将这些助手返回到您的基本Laravel发行版中。