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

Lara_Love's avatar

Undefined array key id

Hello dear friends I get the session information in the controller with dd. But without dd in the error handler Undefined array key Gives / Please help if possible

FORM SEND TO CONTROLLER @php $total = 0 @endphp @if(session('cart')) @foreach(session('cart') as $id => $details) @php $total += $details['price'] * $details['quantity'] @endphp {{ $id }}

                                <input type="hidden" name="name" value="{{ $details['name'] }}">
                                
                                    {{ $details['name'] }}
                                
								<input type="hidden" name="quantity" value="{{ $details['quantity'] }}">
                                    {{ $details['quantity'] }}
                                
                                    <input type="hidden" name="price" value="{{ $details['price'] }}">
                                    {{ $details['price'] }}
                               
                        
                    @endforeach
                    <div >
                          <input type="hidden" name="total" value="{{ $total }}">{{ $total }}
                                $
                     </div>
                    <div>
                        <a href="{{ route('ad') }}" type="submit" >
                           PAY
                        </a>
                    </div>
                @endif

public function ad(Request $request)

{
    $product = $request->session()->get('cart');

// dd($product); $factor = Faktor::create([ 'user_id' => Auth::id(), 'product_id' => $product['id'], ------->Error is Undefined array key id 'total' => $request->total, ]);

    $factor->products()->attach($product);

    return redirect()->back()

dd($product); ^ array:2 [▼ 1 => array:5 [▼ "product_id" => 1 "name" => "ssd samsumg " "quantity" => 1 "price" => "1000" "image" => "20221014072724.jpg" ] 2 => array:6 [▼ "product_id" => 2 "name" => "cpu Gen 12 Intel" "quantity" => 2 "price" => "2000" "image" => "20221014072801.jpg"

] ]

Model Faktor

protected $fillable = [ 'product_id', 'user_id', 'total', ]; public function products() { return $this->belongsToMany(Product::class); }

public function product()
{
    return $this->hasMany(Product::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

Model Product

public function faktors() { return $this->belongsToMany(Faktor::class); }

public function faktor()
{
    return $this->belongsTo(Faktor::class);
}

Table Pivot

Schema::create('product_faktor', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained(); $table->foreignId('faktor_id')->constrained(); });

Table Faktors Schema::create('faktors', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('user_id'); $table->unsignedBigInteger('product_id'); $table->string('total'); $table->string('send')->default('0'); $table->foreign('user_id')->references('id')->on('users') ->onDelete('cascade'); $table->foreign('product_id')->references('id')->on('products') ->onDelete('cascade'); $table->timestamps(); });

0 likes
20 replies
tykus's avatar

Assuming there is more than one product in the Cart, I would expect this

$request->session()->get('cart');

to return an Array of Products in the Cart, right???

Can you dd($request->session()->get('cart')) there and post the result?

1 like
Lara_Love's avatar

@tykus hi dear friend public function ad(Request $request)

{
    $product = $request->session()->get('cart');
	dd($product);
    $factor = Faktor::create([
        'user_id' => Auth::id(),
        'product_id' =>  $product['id'],
        'total' => $request->total,
    ]);

    $factor->products()->attach($product);

    return redirect()->back();

dd($product); show all data that form send.dd is ok. but without dd give Error array id from 'product_id' => $product['id'],

Snapey's avatar

@LoverCode you say that dd shows that all data is present. Your error message says not.

kindly show what $product contains

most likely it contains an array which you need to iterate over

Lara_Love's avatar

@tykus hi dear friend this is

dd($product);

^ array:2 [▼ 1 => array:4 [▼ "name" => "ssd samsumg " "quantity" => 1 "price" => "1000" "image" => "20221014072724.jpg" ] 2 => array:4 [▼ "name" => "cpu Gen 12 Intel" "quantity" => 1 "price" => "2000" "image" => "20221014072801.jpg" ] ]

AungHtetPaing__'s avatar

@LoverCode Your product data doesn't have id. Right?. So you can only get data that exists inside product data. Like this

$product[0]['name']

You need to save product id in your cart session.

1 like
Lara_Love's avatar

in dd we have id now ^ array:2 [▼ 1 => array:5 [▼

"product_id" => 1

"name" => "ssd samsumg "
"quantity" => 1
"price" => "1000"
"image" => "20221014072724.jpg"

] 2 => array:6 [▼

"product_id" => 2

"name" => "cpu Gen 12 Intel"
"quantity" => 2
"price" => "2000"
"image" => "20221014072801.jpg"

] ]

public function ad(Request $request) { $product = $request->session()->get('cart'); dd($product); Faktor::create([ 'user_id' => 1, 'product_id' => $product['id'], --->Undefined array key "id" 'total' => 1, ]); return redirect()->back(); ------------and without dd this error Undefined array key "id"

tykus's avatar

@LoverCode can you understand that the contents of the cart is an array with two products; each Product represented by an associative array (key/value pairs)

[
    [
        "product_id" => 1,
        "name" => "ssd samsumg ",
        "quantity" => 1,
        "price" => "1000",
        "image" => "20221014072724.jpg",
    ],
    [
        "product_id" => 2,
        "name" => "cpu Gen 12 Intel",
        ""quantity" => 2,
        "price" => "2000",
        "image" => "20221014072801.jpg",
    ],
]

Whenever this Cart is assigned to $product, there is no id key at the top level (in fact there is no id key in the nested arrays). So you probably need something like this (assuming you create one Faktor record for each Product in the Cart?

public function ad(Request $request) {
    $products = $request->session()->get('cart');

    foreach ($products as $product) {
        Faktor::create([
            'user_id' => 1,
            'product_id' => $product['product_id'],
            'total' => 1,
        ]);
    }
    return redirect()->back();
}

I don't know if total should be the $product['quantity'], or maybe the subtotal $product['quantity'] * $product['price']?

Lara_Love's avatar

@tykus Hi dear friend After a long time, I just started training. Yes, I want to save all the products that come from the form, but I don't know it completely (I want to save the ones that are in dd)

1 like
tykus's avatar

@LoverCode no idea what that means.

I want to save the ones that are in dd

I have shown you how to iterate over the items in the Cart (the array you dd'ed). What is not working now?

1 like
tykus's avatar

@Aung Htet Paing__ basic PHP seems to be a struggle…

1 like
priyalaks's avatar

You need to iterate your products . Ultimately It will insert all your contents tat u see in your dd in multiple iterations. Try adding the iterations as below - you might get what you want.

$product = $request->session()->get('cart');

	foreach($product as $product_item){
	$factor = Faktor::create([
        'user_id' => Auth::id(),
        'product_id' =>  $product_item['id'],
        'total' => $request->total,
    ]);

    $factor->products()->attach($product_item);
	}
    

    return redirect()->back();
1 like
Lara_Love's avatar

@priyalaks hi dear friend / no its erroe is Undefined array key "id" in controller 'product_id' => $product_item['id'],----->Undefined array key "id"

1 like
AungHtetPaing__'s avatar

@LoverCode did you try tykus code? I think that will not cause Undefined array key "id" error. Then you need to understand php array. As i said in previous reply, you need to save id in your session if you want to use something like this

'product_id' => $product_item['id']

// or

'product_id' => $product['id']

I told you to save 'id' in your session but you saved 'id' with name product_id in your session.

https://www.w3schools.com/php/php_arrays_associative.asp

1 like
priyalaks's avatar

@LoverCode Can you dd and show us $product . Please check if the index id is present in your array . And can you Change it appropriately as per your code . .

2 likes
Lara_Love's avatar

I have brought a new question that I hope we can solve the problem together

Please or to participate in this conversation.