简体   繁体   English

如何在 Laravel 8 中设置默认头像?

[英]How to set a default avatar in Laravel 8?

I'm trying to create a default avatar for my page but only a string appear, not my image.我正在尝试为我的页面创建一个默认头像,但只出现一个字符串,而不是我的图像。 Here's what I do:这就是我所做的:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('avatar')->default('/storage/avatars/avatar.png');
            $table->rememberToken();
            $table->timestamps();
        });
    }

in Migrations
-------------------
 <img src="{{ $page.props.auth.user.avatar }}" alt=""> {{ $page.props.auth.user.avatar }}

In my code
obs: this second {{ $page.props.auth.user.avatar }} is only for show what is coming

And that's my directorys那是我的目录在此处输入图像描述

You need to add something like this to the footer of your site您需要在网站的页脚中添加类似的内容

<script>
    window.user = '{{ auth()->user() }}'
</script>

Then you will be able to access your users attributes in vue.然后您将能够在 vue.xml 中访问您的用户属性。 like this:像这样:

window.user.avatar

Edit: after reading comments;编辑:阅读评论后; did you make sure to add你确定要添加吗

protected $fillable = ['name','avatar'];
protected $appends = ['avatar'];

and

 public function getAvatarAttribute($value)
 {
 return $this->avatar = $value
 }

to your user model?给您的用户 model?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM