有没有更好的方法来获取Laravel当前登录用户的个人资料图像?

时间:2018-04-09 21:03:20

标签: php laravel

目前,我正在尝试创建一个包含用户的用户名和个人资料图片的元素。到目前为止,我已经提出了一个有效的解决方案,但我想知道是否有更好的选择。

我的解决方案:

<li>
    <img src='{{url("storage/uploads/profile_pictures/".Auth::user()-
    >profile_picture)}}'>
    <p>{{ Auth::user()->username }}</p>
</li>

1 个答案:

答案 0 :(得分:1)

我建议改为在用户模型中编写函数或accessor

这样的事情:

class User extends Authenticatable
{
    // ...

    public function profileImagePath()
    {
        return url("storage/uploads/profile_pictures/" . $this->profile_picture);
    }
}

你可以这样使用它:

<img src='{{auth()->user()->profileImagePath()}}'>