【发布时间】:2018-02-20 10:47:30
【问题描述】:
我的数据库中有我的图像path,现在,我正试图将图像放入我的 html 网格中。但是当我尝试获取图像时,我得到一个错误
此集合实例上不存在属性 [路径]。
但是,我的数据库表中有路径属性。一定是我的模型有错误吗?我的模型对我来说似乎是正确的。无论如何,我不在这里做什么,好吗?
感谢您的关心
HTML
<figure>
<div class="snipcart-item block">
<div class="snipcart-thumb">
<a href="{{ route('product.view', $product->slug)}}"><img src="{{$product->images->path}}" alt=" " class="img-responsive" /></a>
<p>{{$product->name}}</p>
<h4>GH₵ {{ number_format($product->price,2) }} </h4>
</div>
<div class="snipcart-details top_brand_home_details">
<form action="#" method="post">
<fieldset>
</fieldset>
</form>
</div>
</div>
</figure>
图片
public function products()
{
return $this->belongsTo(Product::class);
}
产品图片
public function images()
{
return $this->hasMany(Image::class);
}
【问题讨论】:
-
给你看表
images -
你的关系可能有错误
-
你的 Laravel 版本是多少?
-
@GulmuhammadAkbari,laravel 5
-
Images 返回
Collection,参见laravel.com/docs/5.6/eloquent-relationships#one-to-many。您需要遍历所有现有图像。