Laravel 8: 尝试获取 属性 'price' 的非对象
Laravel 8: Trying to get property 'price' of non-object
我正在尝试使用 Laravel 8 建立一个在线商店,对于我的 cart.blade.php
,我编写了以下代码:
<div class="text-right mt-4">
<label class="text-muted font-weight-normal m-0">Final Price</label>
@php
$totalPrice = Cart::all()->sum(function($cart) {
return $cart['product']->price * $cart['quantity'];
});
@endphp
<div class="text-large"><strong>{{ $totalPrice }} is price</strong></div>
</div>
方法 all()
是这样的:
public function all()
{
$cart = $this->cart;
$cart = $cart->map(function($item) {
return $this->withRelationshipIfExist($item);
});
return $cart;
}
那么如何解决这个问题呢?我真的很感激你们的任何想法或建议...
提前致谢。
错误告诉您 $cart 不是一个对象。当设置不正确时会发生这种情况。
如果你的 all() 函数在你的 Cart 模型中,那么你实际上不需要 $this->cart。你只需要 $cart = $this.
我正在尝试使用 Laravel 8 建立一个在线商店,对于我的 cart.blade.php
,我编写了以下代码:
<div class="text-right mt-4">
<label class="text-muted font-weight-normal m-0">Final Price</label>
@php
$totalPrice = Cart::all()->sum(function($cart) {
return $cart['product']->price * $cart['quantity'];
});
@endphp
<div class="text-large"><strong>{{ $totalPrice }} is price</strong></div>
</div>
方法 all()
是这样的:
public function all()
{
$cart = $this->cart;
$cart = $cart->map(function($item) {
return $this->withRelationshipIfExist($item);
});
return $cart;
}
那么如何解决这个问题呢?我真的很感激你们的任何想法或建议...
提前致谢。
错误告诉您 $cart 不是一个对象。当设置不正确时会发生这种情况。
如果你的 all() 函数在你的 Cart 模型中,那么你实际上不需要 $this->cart。你只需要 $cart = $this.