error checkout laravel shopping cart

mau nanya nih. lagi belajar buat shopping cart. pake packagenya "gloudemans/shoppingcart" ketika waktu checkout terjadi error seperti ini kira2 salahnya dimana ya gan?

ErrorException in ShopController.php line 52: Trying to get property of non-object

in ShopController.php line 52 at HandleExceptions->handleError('8', 'Trying to get property of non-object', 'C:\xampp\htdocs\mw\app\Http\Controllers\ShopController.php', '52', array('id' => 'checkout', 'product' => null)) in ShopController.php line 52 at ShopController->addToCart('checkout') at call_user_func_array(array(object(ShopController), 'addToCart'), array('id' => 'checkout')) in Controller.php line 80 at Controller->callAction('addToCart', array('id' => 'checkout')) in ControllerDispatcher.php line 146 at ControllerDispatcher->call(object(ShopController), object(Route), 'addToCart') in ControllerDispatcher.php line 94 at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96 at ControllerDispatcher->callWithinStack(object(ShopController), object(Route), object(Request), 'addToCart') in ControllerDispatcher.php line 54

 public function CheckOut(){
        $cart_content = Cart::content(1);

        foreach ($cart_content as $cart) {

            $transaction  = new Transaksi();

            $product = Produk::find($cart->id);

            $transaction->produk_id  = $cart->id;
            $transaction->user_id    = Auth::user()->id;
            $transaction->qty         = $cart->qty;
            $transaction->total_price = $cart->price * $cart->qty;
            $transaction->status      = 'unpaid';
            $transaction->save();

        }
        Cart::destroy();
        return redirect()->route('shop');
    }
 public function addToCart($id){
        $product = Produk::find($id);

        $id          = $product->id;
        $name        = $product->name;
        $qty         = 1;
        $price       = $product->price;

        $data = array('id'          => $id,
                      'name'        => $name,
                      'qty'         => $qty,
                      'price'       => $price
                      );

        Cart::add($data);
        $cart_content = Cart::content(1);
        return View ('shop.showcart', ['cart_content' => $cart_content);
    }

avatar imansetyawan
@imansetyawan

21 Kontribusi 0 Poin

Diperbarui 7 tahun yang lalu

2 Jawaban:

keterangan errornya ada pada baris 52 di ShopController.php, bisa ditambahkan informasinya baris ke 52 di halaman itu yang mana yang dicodenya?

avatar hilmanski
@hilmanski

2672 Kontribusi 2132 Poin

Dipost 7 tahun yang lalu

public function addToCart($id){  <--line 48
        $product = Produk::find($id);

        $id          = $product->id; <--line 51
        $name        = $product->name;
        $qty         = 1;
        $price       = $product->price;

        $data = array('id'          => $id,
                      'name'        => $name,
                      'qty'         => $qty,
                      'price'       => $price
                      );

        Cart::add($data);
        $cart_content = Cart::content(1);
        return View ('shop.showcart', ['cart_content' => $cart_content);
    }

@hilmanrdn itu gan

avatar imansetyawan
@imansetyawan

21 Kontribusi 0 Poin

Dipost 7 tahun yang lalu

Login untuk ikut Jawaban