Laravel:非对象的属性
试图通过“尝试获取非对象的属性”来解决这个问题,我几乎通过将所有$item->product\u id更改为$item['product\u id')来完成,并且除了一个对象之外,它工作正常。我有一个图像url需要修复,以及我不知道如何做到这一点 代码如下:Laravel:非对象的属性,laravel,url,Laravel,Url,试图通过“尝试获取非对象的属性”来解决这个问题,我几乎通过将所有$item->product\u id更改为$item['product\u id')来完成,并且除了一个对象之外,它工作正常。我有一个图像url需要修复,以及我不知道如何做到这一点 代码如下: <img src="{{ asset($item->product->productImages->first()->path) }}" alt="img"> product->productImage
<img src="{{ asset($item->product->productImages->first()->path) }}" alt="img">
product->productImages->first()->path)}“alt=“img”>
使用此代码
@if(!empty($item->product->productImages->first()))
<img src="{{ asset($item->product->productImages->first()->path) }}" alt="img">
@endif
@if(!empty($item->product->productImages->first())
product->productImages->first()->path)}“alt=”img“>
@恩迪夫
$item->product
或$item->product->productImages->first()
正在返回null
,您无法访问null
的属性。调试代码;永远不要假设$object->property
是有效代码。它不会返回Null。当I dd()时,我可以看到一个正确的映像路径,这是导致错误的代码行吗?这是一种循环吗?$item
始终是同一个变量,还是基于数据库查询?如果你说这行没有问题,你就必须提供更多的细节。我只是想问一下,如何像我对剩余部分资产($item->product->productImages)所做的那样,将->first()和->路径更改为asset($item['product']['productImages']是的,这行代码造成了问题,因为当我删除它时,它工作正常。是的,它在一个循环中。我需要渲染正确的图像路径