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?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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(); });
Please or to participate in this conversation.