这是我的index.blade.php
<img class="img-circle img-bordered-sm" src="{{$user->photo_id ? $user->photo->file : 'http://placehold.it/100x100'}}" alt="user image">
最佳答案
在后模型中添加以下内容:
public function getWriterAvatarAttribute(){
try {
return User::findOrFail($this->user_id)->photo->file;
} catch (ModelNotFoundException $e){
return 'http://placehold.it/100x100';
}
}
并在 View 中:
<img src="{{$post->writer_avatar}}" alt="user image">
关于php - 如何根据laravel上博客文章的user_id显示用户的照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54547132/