【发布时间】:2021-07-27 14:57:55
【问题描述】:
我想在我的 Blade 上显示上传的用户图片,所以我添加了这个:
@foreach($users as $user)
@if($user->photo_id)
<td class="center">
<img src="{{ $user->photos->path }}" width="80">
</td>
@else
<td class="center">
</td>
@endif
@endforeach
但它返回此错误:
异常此集合实例上不存在属性 [路径]。
我已经将这些关系添加到模型中:
Photo.php:
public function user()
{
return $this->belongsTo(User::class);
}
User.php:
public function photos()
{
return $this->hasMany(Photo::class);
}
那么这里出了什么问题?我该如何解决这个问题?
当我说:{{ dd($user->photos) }} 时,它会返回:
Illuminate\Database\Eloquent\Collection {#1231 ▼
#items: []
}
这里还有表photos:
【问题讨论】: