dimanche 10 septembre 2017

Undefined property: Illuminate\Database\Eloquent\Builder::$id Laravel 5.4

What I want to is add a product to the shopping cart. After i click add to cart button , i get the undefined property error. I take this code from http://ift.tt/2eY419X. Can someone clarify what is the error. Thanks :)

Product.blade.php

  @foreach ($id as $id)
    <div class="caption">
     <div class="row">
      <div class="col-md-6 col-xs-6">
      <h3></h3>
      </div>
      <div class="col-md-6 col-xs-6 price">
       <h3>
       <label>RM </label></h3>
         </div>
     <div class="col-md-6 col-md-offset-3">
     <a href="" class="btn btn-
     success btn-product"><span class="fa fa-shopping-cart"></span>Add to 
     Cart</a></div>
         </div>
         </div>
           </div>
           </div>
          @endforeach

CartController.php

public function __construct()
{$this->middleware('auth');}

public function addItem ($productId){

   $cart = Cart::where('user_id',Auth::user()->id);

   if(!$cart){
       $cart =  new Cart();
       $cart->user_id=Auth::user()->id;
       $cart->save();
   }

   $cartItem  = new Cartitem();
   $cartItem->product_id=$productId;
   $cartItem->cart_id= $cart->id;
   $cartItem->save();
   return redirect('/viewcart');
   }


 public function showCart(){

 $cart = Cart::where('user_id',Auth::user()->id);

   if(!$cart){
       $cart =  new Cart();
       $cart->user_id=Auth::user()->id;
       $cart->save();
   }

   $items = $cart->cartItems;
   $total=0;
   foreach($items as $item){
       $total+=$item->product->price;
   }

   return view('viewcart',['items'=>$items,'total'=>$total]);}

Viewcart.blade.php

 @foreach ($items as $item)

 <tr><td class="col-sm-8 col-md-6"> <div class="media">
 <a class="thumbnail pull-left" href="#"> <img class="media-object" src="
 " style="width: 100px; height: 72px;"> </a>
 <div class="media-body">
 <h4 class="media-heading"><a href="#"></a></h4>
 </div></div></td>    
 <td class="col-sm-1 col-md-1 text-center"><strong>
  </strong></td>
 <td class="col-sm-1 col-md-1">
 <a href="/removeItem/"> <button type="button" class="btn btn-
 danger">
 <span class="fa fa-remove"></span> Remove
  </button></a></td></tr>
      @endforeach



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire