Because you try to add to an array key that does not exist
Add this at the start of the loop
if (! isset($product_info['prod_id'][$iter]) {
$product_info['prod_id'][$iter] = 0;
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
CheckoutController.php
$cart = Cart::with('Product')->where('user_id', auth()->user()->id)->get();
// prod info cart
$iter = 0;
$product_info = array();
// dd($cart);
foreach($cart as $value)
{
$product_info['prod_id'][$iter] += $value->prod_id;
$product_info['prod_unit'][$iter] += $value->amount;
$product_info['prod_type'][$iter] += $value->prod_type;
$iter++;
}
ref: https://laracasts.com/discuss/channels/laravel/how-to-fix-message-undefined-index-product-id
I wonder why the error appears on this line: $product_info['prod_id'][$iter] += $value->prod_id;
@davy_yg Can you explain why you want to add the product ids into a new number ? I dont think I get the idea
$product_info['prod_id'][$iter] += $value->prod_id;
Same with product type. I assume they are numbers, as you want to add them together (+ means add two numbers together)
Please or to participate in this conversation.