试图访问Laravel购物车中null类型值的数组偏移量



当我点击购物车按钮时,它显示错误'试图访问Laravel中null类型值的数组偏移'。

IlluminateFoundationBootstrapHandleExceptions::handleError
C:xampphtdocslaravel-ecommerceappListenersCartUpdatedListener.php:30 
public function handle($event)
{
$couponName = session()->get('coupon')['name'];
if ($couponName) {
$coupon = Coupon::where('code', $couponName)->first();
dispatch_now(new UpdateCoupon($coupon));
}
}

请帮忙。提前感谢

试试这个

public function handle($event)
{
$session_coupon = session()->get('coupon');
if($session_coupon && isset($session_coupon['name'])){
$coupon = Coupon::where('code', $session_coupon['name'])->first();
dispatch_now(new UpdateCoupon($coupon));
} else {
dd("if $session_coupon is null or $session_coupon has doesn't exist 'name' key");
}
}

这是因为我的会话优惠券不存在。因此,我在脚本中添加了空验证。错误已解决

修复电子商务listener CartUpdatedListener错误

https://laracasts.com/discuss/channels/laravel/fix-ecommerce-listenerscartupdatedlistener-error

public function handle($event)
{
$couponName = session()->get('coupon')['name']??null;
if ($couponName) {
$coupon = Coupon::where('code', $couponName)->first();
dispatch_now(new UpdateCoupon($coupon));
}
}

最新更新