试图获得 属性 'price' 的非对象
Trying to get property 'price' of non-object
我正在与 Laravel 5.8 合作开发我的项目,在这个项目中,我想检查用户的购物车价格是否高于自定义数字(也应该从数据库中获取) ), 然后打印 Your delivery is free
.
所以为了做到这一点,我添加了这个:
@foreach(\App\Shop\ProductDelivery::find(1) as $delivery)
@if($cartPrice >= $delivery->price)
<i>
You delivery is free
</i>
@endif
@endforeach
但是它向我显示了这个错误:
正在尝试获取 属性 'price' 的非对象
参考这一行:
<?php if($cartPrice >= $delivery->price): ?>
但是价格已经存在于数据库中:
那么这里出了什么问题?我该如何解决这个问题?
\App\Shop\ProductDelivery::find(1)
只是 return 一条记录,因此在这种情况下使用 foreach 会导致您的问题。要解决它,您可以将代码更改为:
@php
$delivery = \App\Shop\ProductDelivery::find(1)
@endphp
@if($delivery && $cartPrice >= $delivery->price)
<i>
You delivery is free
</i>
@endif
或者如果您仍然想使用 foreach,尽管它不是必需的:
@foreach(\App\Shop\ProductDelivery::where('id', 1)->get() as $delivery)
@if($cartPrice >= $delivery->price)
<i>
You delivery is free
</i>
@endif
@endforeach
我正在与 Laravel 5.8 合作开发我的项目,在这个项目中,我想检查用户的购物车价格是否高于自定义数字(也应该从数据库中获取) ), 然后打印 Your delivery is free
.
所以为了做到这一点,我添加了这个:
@foreach(\App\Shop\ProductDelivery::find(1) as $delivery)
@if($cartPrice >= $delivery->price)
<i>
You delivery is free
</i>
@endif
@endforeach
但是它向我显示了这个错误:
正在尝试获取 属性 'price' 的非对象
参考这一行:
<?php if($cartPrice >= $delivery->price): ?>
但是价格已经存在于数据库中:
那么这里出了什么问题?我该如何解决这个问题?
\App\Shop\ProductDelivery::find(1)
只是 return 一条记录,因此在这种情况下使用 foreach 会导致您的问题。要解决它,您可以将代码更改为:
@php
$delivery = \App\Shop\ProductDelivery::find(1)
@endphp
@if($delivery && $cartPrice >= $delivery->price)
<i>
You delivery is free
</i>
@endif
或者如果您仍然想使用 foreach,尽管它不是必需的:
@foreach(\App\Shop\ProductDelivery::where('id', 1)->get() as $delivery)
@if($cartPrice >= $delivery->price)
<i>
You delivery is free
</i>
@endif
@endforeach