Неопределенное свойство: Illuminate \ Database \ Eloquent \ Builder :: $ id Laravel 5.4

Я хочу добавить товар в корзину. После нажатия кнопки «Добавить в корзину» я получаю неопределенную ошибку свойства. Я беру этот код из https://www.codetutorial.io/the-shopping-cart-how-to-craft-an-e-shop-with-laravel/. Может кто-нибудь уточнить, в чем заключается ошибка. Спасибо 🙂

Product.blade.php

  @foreach ($id as $id)
<div class="caption">
<div class="row">
<div class="col-md-6 col-xs-6">
<h3>{{$id->name}}</h3>
</div>
<div class="col-md-6 col-xs-6 price">
<h3>
<label>RM {{$id->price}}</label></h3>
</div>
<div class="col-md-6 col-md-offset-3">
<a href="{{ route('addProduct',$id->product_id) }}" 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="{{$item->product->image}}" style="width: 100px; height: 72px;"> </a>
<div class="media-body">
<h4 class="media-heading"><a href="#">{{$item->product->name}}</a></h4>
</div></div></td>
<td class="col-sm-1 col-md-1 text-center"><strong>{{$item->product>price}}
</strong></td>
<td class="col-sm-1 col-md-1">
<a href="/removeItem/{{$item->id}}"> <button type="button" class="btn btn-
danger">
<span class="fa fa-remove"></span> Remove
</button></a></td></tr>
@endforeach

Обновить
база данных cart_items

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector