在Laravel中显示具有关系的图像



我想通过用户的照片关系来显示,但它没有显示这张照片也存在,但当我拍摄dd((时,它显示为null这是Blade代码,除了user_image 之外,它们都能工作

@foreach($threads as $thread)
<div class="card my-3  rounded-lg mr-  ">
<div class="card-body shadow ">
<blockquote class="blockquote mb-0">
<div class=" d-flex">
<p style="font-size: x-large; font-weight: bold; "><a class="" href="/threads/{{$thread->slug}}">{{IlluminateSupportStr::limit($thread->title,50)}}</a>
</p>
</div>
<div class=" d-flex">
<img class="img-profile rounded-circle mr-1 " style="width: 60px; height: 60px;"

src="{{$thread->user->user_image}}">      {{--    this is not working--}}
<p class="mt-3 text-secondary"
توسط {{$thread->user->name}} آپدیت شد</p>
</div>
<footer
class="blockquote-footer text-dark">{{IlluminateSupportStr::limit($thread->description,150)}}
</blockquote>
</div>
</div>
@endforeach

也许可以取代

public function user()
{
return $this->belongsTo(User::class);
}
by something like
public function images()
{
return $this->belongsTo(User::class);
}

对于用户做

hasMany (images)

此外,您还必须进行迁移才能添加外键,您做到了吗?

相关内容

  • 没有找到相关文章

最新更新