php - 如何根据 laravel 上博客文章的 user_id 显示用户的照片?
问题描述
这是我的 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';
}
}
并认为:
<img src="{{$post->writer_avatar}}" alt="user image">
推荐阅读
- json - 在 Swagger 2.0 中,数组参数中的“默认”实际上是什么意思?
- r - R - 无法在 lapply 函数中进行 rbind
- python - 你如何在输入中连接?
- python - Python regex bool 测试字符串列表
- jquery - 是否可以禁止用户删除 select2 选择列表中的特定值?
- vb.net - vb.net如果不是整数,如何拒绝用户输入
- python - python3获取strptime的增量
- powershell - PowerShell - 参数 [Int32]$Month 在应该有前导 0 时显示为单个整数
- sql-server - 需要帮助 Sql Server 当前日期查询
- qnamaker - QnAMaker 是从答案中学习,还是仅从问题中学习?