0

When I Am trying to access API and add the protected $appends = ['avatar'] it gives me the error while in case I remove the $appends the error go.

<?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();
   }
 }

When I go to the network trace I see this tip -> message: "Method Illuminate\Database\Query\Builder::getAvatarAttribute does not exist."

1 Answers1

1

Just checked your code you have to declare as

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

Attribute instead of atribute

user1900956
  • 487
  • 2
  • 9