Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Undefined index: prod_id

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;

0 likes
17 replies
Sinnbeck's avatar

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;
} 
davy_yg's avatar
Level 27

I still get the same error and the value of dd($value->prod_id); is 1 if I took of the comment.

ErrorException Undefined index: prod_id

	foreach($cart as $value)
    	{
        // dd($value->prod_id);
        if(! isset($product_info['prod_id'][$iter])){    
            $product_info['prod_id'][$iter] += $value->prod_id;
            }
        	$product_info['prod_unit'][$iter] += $value->amount;
        	$product_info['prod_type'][$iter] += $value->prod_type;  

        	$iter++;  
    	}
Sinnbeck's avatar

@davy_yg That wasnt what I said?

	foreach($cart as $value)
    	{
        // dd($value->prod_id);
        if(! isset($product_info['prod_id'][$iter])){    
            $product_info['prod_id'][$iter] = 0;
            }
        	$product_info['prod_unit'][$iter] += $value->amount;
        	$product_info['prod_type'][$iter] += $value->prod_type;  

        	$iter++;  
    	}
davy_yg's avatar
Level 27

@Sinnbeck

This time the error move to the next line: Undefined index: prod_unit

Should I define the value manually?

I thought it should sum up the value from:

+= $value->prod_id since it is not null or 0.

+= $value->amount;

+= $value->prod_type;

Sinnbeck's avatar

@davy_yg of course it fails. I gave you an example of how to fix it and assumed you would understand the code. You need to do it for each one

1 like
siangboon's avatar

seriously, personally I think that is a sincere and good advice to you ..

1 like
davy_yg's avatar
Level 27

@siangboon The thing is you are not my boss, you are just someone that I do not know. The world is big there are diverse kind of people here and even diverse kind of leaders.

Never compare the sun and moon, they will shine when it's their time.

siangboon's avatar

it's ok, I'm just nobody...

I remember that I spent quite some times to help answer your number of questions in the pass few years although most were not the "best answer" but only able to find these 2 as it's in my list as a proof, and I had been rarely wasting time on looking on your questions since a long times ago as found that those questions supposed to resolved and asked before keep repeating ask again and again, furthermore you never thanks and appreciate peoples' helps and good advice but always take it as granted and keep asking for solution only instead of trying solving/thinking by yourself.

https://laracasts.com/discuss/channels/laravel/helpers

https://laracasts.com/discuss/channels/laravel/online-shop

anyway, I'm not here to argue. Wish you all the best and hope you will shine one day very soon.

2 likes
frankielee's avatar

I mean you could define the array with the initial value first?

Example

$product_info = [
	"id"=>0,
"hello"=>0,
"world"=>0
];

Snapey's avatar

You are not adding anything. You copy the $cart items into $product_info, nothing more.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@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)

davy_yg's avatar
Level 27

@Sinnbeck lol, I think it suppose to be something like these:

	$iter = 0;

    $product_info = array();

    foreach($cart as $value)
    {
        if(! isset($product_info['prod_id'][$iter])){    
            $product_info['prod_id'][$iter] = 0;
        }
        if(! isset($product_info['prod_unit'][$iter])){    
            $product_info['prod_unit'][$iter] = 0;
        }
        if(! isset($product_info['prod_type'][$iter])){    
            $product_info['prod_type'][$iter] = 0;
        }

        $product_info['prod_id'][$iter] = $value->prod_id;
        $product_info['prod_unit'][$iter] = $value->amount;
        $product_info['prod_type'][$iter] = $value->prod_type;  

        $iter++;  
    }

later on in the program I am going to do this: (notes that the variable has changed from $product_info to $info as they are being passed on to another function).

		if(isset($info['prod_type'][$iter])){
        foreach($info['prod_id'] as $product){
            OrderProduct::create([
                'order_id' => $id,
                'prod_id' => $product,
                'prod_unit' => $info['prod_unit'][$iter],
                'prod_type' => $info['prod_type'][$iter]
            ]);

        }
        $iter++;
    } else {
        foreach($info['prod_id'] as $product){
            OrderProduct::create([
                'order_id' => $id,
                'prod_id' => $product,
                'prod_unit' => $info['prod_unit'][$iter],
                'prod_type' => null
            ]);

        }
        $iter++;

    }
sidanalavi's avatar

I don't know why you are adding the product id , unit and type as in each loop that error will come for sure as you are incrementing the iteration and product_info is a empty array . If going to remain empty as it is then you should go with below solution also i have made a small tweak in the code check the output see if that's what you needed. Otherwise please explain what you are trying to achieve.

$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[$iter]['prod_id'] = $value->prod_id;
        $product_info[$iter]['prod_unit'] = $value->amount;
        $product_info[$iter]['prod_type'] = $value->prod_type;  

        $iter++;  
    }

Please or to participate in this conversation.