when i click shopping cart button it show error 'Trying to access array offset on value of type null in Laravel'.
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
C:\xampp\htdocs\laravel-ecommerce\app\Listeners\CartUpdatedListener.php:30
public function handle($event)
{
$couponName = session()->get('coupon')['name'];
if ($couponName) {
$coupon = Coupon::where('code', $couponName)->first();
dispatch_now(new UpdateCoupon($coupon));
}
}
Please kindly help. Thank in advance
CodePudding user response:
Try this
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");
}
}
CodePudding user response:
It is occur because my session coupon doesn't exists. so, i add null validation in script. Error was solved
Fix ecommerce Listeners\CartUpdatedListener error
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));
}
}