如何根据laravel上博客文章的user_id显示用户的照片?

时间:2019-02-06 05:29:41

标签: php laravel-5

这是我的index.blade.php

Point Point::operator%( double angle)
{
    double rad=angle * PI / 180.0;
    double s = sin(rad);
    double c = cos(rad);

    // rotate point
    return Point(this->x * c - this->y * s,
                 this->x * s + this->y * c);
}

2 个答案:

答案 0 :(得分:2)

在帖子模型中添加以下内容:

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">

答案 1 :(得分:0)

像这样使用:

<img src={{$user->photo ? $user->photo->file : 'http://placehold.it/100x100'}}" alt="user image">