Tahun baru, skill baru! 🚀. Masukkan kupon "skill2025" untuk diskon 30% di kelas apa saja

Dapat pesan Error Undefined index: product_price

gan mohon bantuannya, saya dapat pesan error pada saat saya menjalankan file cart.blade.php

ini pesan errornya : ErrorException Undefined index: product_price

ini controllernya : public function addToCart(Request $request) { $this->validate($request, [ 'product_id' => 'required|exists:products,id', 'qty' => 'required|integer' ]);

$carts = $this->getCarts(); if ($carts && array_key_exists($request->product_id, $carts)) { $carts[$request->product_id]['qty'] += $request->qty; } else { $product = Product::find($request->product_id); $carts[$request->product_id] = [ 'qty' => $request->qty, 'product_id' => $product->id, 'product_name' => $product->name, 'product_price' => $product->price, 'product_image' => $product->image ]; }

$cookie = cookie('dw-carts', json_encode($carts), 2880); return redirect()->back()->cookie($cookie); }

public function listCart() { $carts = $this->getCarts(); $subtotal = collect($carts)->sum(function($q) { return $q['qty'] * $q['product_price']; }); return view('ecommerce.cart', compact('carts', 'subtotal')); }

ini cart.blade.php @if(! empty($carts)) @forelse ($carts as $row)

![{{ $row['product_name'] }}]({{ asset('storage/products/' . $row['product_image']) }})

{{ $row['product_name'] }}

Rp {{ number_format($row['product_price']) }}
Rp {{ number_format($row['product_price'] * $row['qty']) }}

@empty

Tidak ada belanjaan

@endforelse @endif

Mohon bantuannya :)

avatar adam1001
@adam1001

1 Kontribusi 0 Poin

Diperbarui 4 tahun yang lalu

1 Jawaban:

Coba di debug dulu, property product_price-nya ada ngga, buat debugnya bisa kaya gini :

<pre> $carts = $this-&gt;getCarts(); if ($carts && array_key_exists($request-&gt;product_id, $carts)) { $carts[$request-&gt;product_id]['qty'] += $request-&gt;qty; } else { $product = Product::find($request-&gt;product_id);

dd($product); // &lt;-- Fungsi dd untuk debug pada laravel

$carts[$request-&gt;product_id] = [ 'qty' =&gt; $request-&gt;qty, 'product_id' =&gt; $product-&gt;id, 'product_name' =&gt; $product-&gt;name, 'product_price' =&gt; $product-&gt;price, 'product_image' =&gt; $product-&gt;image ]; } </pre>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban