Error trying to get property of non object

mau nanya gan, ketika saya jalankan function checkout kenapa yang dikeluarkan error trying to get property non object line 86, padahal yang saya jalankan adalah function checkout. mohon bantuan nya gan. thanks

 public function addToCart(Request $request, $id){  <<-- Line 79
        $this->data['kategorishop'] = Kategori::all();
        $this->data['kategoris'] = Kategori2::all();
        $this->data['departemens'] = Departemen::orderBy('created_at', 'DESC')->take(4)->get();
        $this->data['infos'] = Info::all();

        $product = Produk::find($id);
        $id          = $product->id;  <<-- Line 86
        $name        = $product->name;
        $price       = $product->price;
        $qty         = $request->qty;

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

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

    public function CheckOut(){
        $cart_content = Cart::content();
        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->qty) * ($cart->price);
            $transaction->status      = 'unpaid';
            $transaction->save();

        }
avatar imansetyawan
@imansetyawan

21 Kontribusi 0 Poin

Diperbarui 7 tahun yang lalu

2 Jawaban:

coba gan, function addToCart nya ganti jadi gini

 public function addToCart(Request $request, $id){  <<-- Line 79
        $this->data['kategorishop'] = Kategori::all();
        $this->data['kategoris'] = Kategori2::all();
        $this->data['departemens'] = Departemen::orderBy('created_at', 'DESC')->take(4)->get();
        $this->data['infos'] = Info::all();

        $product = Produk::where('id', $id)->first();
        $id          = $product->id;  <<-- Line 86
        $name        = $product->name;
        $price       = $product->price;
        $qty         = $request->qty;

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

        Cart::add($data);
        $cart_content = Cart::content();
        return View ('shop.showcart', $this->data, ['cart_content' => $cart_content]);
    }
avatar ahnafhf21
@ahnafhf21

107 Kontribusi 66 Poin

Dipost 7 tahun yang lalu

error nya bukan pas di addtocart nya sih gan, karena addtocart nya udah berhasil masuk ke tabel cartnya gan, ketika dilakukan fungsi checkout nah malah error nya mengarah ke error fungsi addtocart padahal fungsi addtocartnya ketika dijalankan berhasil berjalan. dan ketika di dd di fungsi addtocart juga $id udah dapat. kira2 kenapa ya gan?

ini untuk blade cartnya

 <form action="{{route('checkout_cart')}}" method="post">
                      <div class="table-responsive">
                        <table class="table">
                          <thead>
                            <tr>
                              <th>Image</th>
                              <th>Product Name</th>
                              <th>Price</th>
                              <th>Quantity</th>
                              <th>Sub Total</th>
                              <th>Action</th>
                            </tr>
                          </thead>
                          <tbody>

                        @foreach($cart_content as $cart)
                        <tr>
                          <td class="col-xs-2">
                            <span class="cartImage"><img src="{{asset('image/product/'.$cart->image)}}" alt="image"></span>
                          </td>
                          <td class="col-xs-4">{{ $cart->name }}</td>
                          <td class="col-xs-2">{{ $cart->price }}</td>
                          <td class="col-xs-2">{{ $cart->qty }}</td>
                          <td class="col-xs-2"></td>
                          <td><a href="{{ route('delete_cart', ['rowId' => $cart->rowId])}}" button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></a></td>
                        </tr>
                         @endforeach
                      </tbody>
                    </table>
                  </div>

                  <div class="row totalAmountArea" style="margin-left: 0px;">
                    <div class="col-sm-4 col-sm-offset-8 col-xs-12">
                      <ul class="list-unstyled">
                        <li>Total <span></span> {{Cart::subtotal()}}</li>
                      </ul>
                    </div>
                  </div>
                  <div class="checkBtnArea">

                    <input type="hidden" name="_token" value="{{csrf_token()}}">
                    <button class="btn btn-primary btn-block">checkout<i class="fa fa-arrow-circle-right" aria-hidden="true"></i></button>
                  </div>
                </form>
avatar imansetyawan
@imansetyawan

21 Kontribusi 0 Poin

Dipost 7 tahun yang lalu

Login untuk ikut Jawaban