当我尝试访问API并添加受保护的$ appends = ['avatar']时,它给了我错误,而如果我删除了$ appends,则出错了。
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'email', 'password',
];
protected $appends = ['avatar'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function posts()
{
return $this->hasMany(Post::class);
}
public function getAvatar(){
return 'https://gravatar.com/avatar/'.md5($this->email).'/?d=mm&s=45';
}
public function getAvatarAtribute(){
return $this->getAvatar();
}
}
当我转到网络跟踪时,我看到此提示->消息:“方法Illuminate \ Database \ Query \ Builder :: getAvatarAttribute不存在。”
答案 0 :(得分:1)
只需检查您必须声明为
的代码 public function getAvatarAttribute(){
return $this->getAvatar();
}
属性而不是属性