Laravel 5.6-> POST http://127.0.0.1:8000/posts 500(内部服务器错误)

时间:2018-10-03 08:59:38

标签: laravel-5 http-status-code-500

当我尝试访问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不存在。”

1 个答案:

答案 0 :(得分:1)

只需检查您必须声明为

的代码
 public function getAvatarAttribute(){
   return $this->getAvatar();
   }

属性而不是属性