我是 Laravel 新手
我有两个表 1- 产品 2- Product_Price 我想在产品页面中显示最后一个产品价格,但我尝试不同的方法,但出现错误
我的控制器代码:
public function show(Request $request, Product $product): View
{
$this->authorize('view', $product);
$ProductTags = $product->ProductTags;
$ProductPrice = $product->ProductPrices->last();
return view('app.products.show', compact('product','ProductTags','ProductPrice'));
}
我的刀片代码是:
@foreach ($ProductPrice as $productprice)
<div>
{{ $productprice->price}}
</div>
@endforeach
但我收到此错误:尝试读取 bool 上的属性“价格”
我收到此错误:尝试读取 bool 上的属性“价格”
$ProductPrice = $product->ProductPrices->last();
这给你一个单一的对象,而不是一个集合。您不需要在视图中循环来获取对象值。
<div>
{{ $ProductPrice->price }}
</div>
这将给出预期的输出。
当循环对象时,循环变量的值为布尔值。因此你会得到错误。