简体   繁体   中英

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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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