【问题标题】:How to set a default avatar in Laravel 8?如何在 Laravel 8 中设置默认头像?
【发布时间】:2022-01-27 18:55:59
【问题描述】:

我正在尝试为我的页面创建一个默认头像,但只出现一个字符串,而不是我的图像。这是我的工作:

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

那是我的目录

【问题讨论】:

  • $page.props.auth.user.avatar 不是有效的 PHP 代码。
  • 我在 Vue 中使用 Laravel Breeze,我的用户名显示为 $page.props.auth.user.name
  • 好吧,不熟悉。

标签: php laravel laravel-8


【解决方案1】:

您需要在网站的页脚中添加类似的内容

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

然后你就可以在 vue 中访问你的用户属性了。像这样:

window.user.avatar

编辑: 读完cmets;你确定添加了吗

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

在您的模型中。所有模型都应该有

protected $fillable = [] (添加归因于您希望能够直接从控制器保存的数组的模型,即'name', 'age', 'gender' 等)或

protected $guarded = [] (添加您不希望可编辑的属性,即'password', 'token' 等)在文档中阅读有关mass assignment 的更多信息。 和

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

到你的模型。

当您将模型发送到前端时,将蛇形情况下function get_______Attribute() 的返回值添加到 json 中。 check it out in the docs

【讨论】:

  • 我把这个功能和这个保护放在哪里?
  • 在您的模型中。所有模型都应该有protected $fillable = [](添加模型归因于您希望能够直接从控制器保存的数组,即'name'、'age'、'gender'等)或protected $guarded = [](添加属性您不希望可编辑,即“密码”、“令牌”等)read more about mass assignment in the docs.
  • 并在您将模型发送到前端时附加添加 function get_______Attribute() 在蛇案例中的返回值到 json。 check it out in the docs
猜你喜欢
  • 1970-01-01
  • 2017-08-16
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 2021-04-21
  • 1970-01-01
相关资源
最近更新 更多