Reputation: 35
When I try to make a function to show the user's avatar I can not solved this error.
I need help! thanks
code:
class User extends Authenticatable
{
....
public function image()
{
return $this->morphOne(Image::class, 'imageable');
}
public function getAvatar()
{
if($this->image->url)
{
$image = $this->image->url;
}else{
$image = "https://www.gravatar.com/avatar/" . md5( strtolower( trim( $this->email ) ) ) . "?d=mp";
}
return $image;
}
}
Upvotes: 1
Views: 65
Reputation: 2951
You can fix the code to that:
class User extends Authenticatable
{
....
public function image()
{
return $this->morphOne(Image::class, 'imageable');
}
public function getAvatar()
{
if($this->image)
{
$image = $this->image->url;
}else{
$image = "https://www.gravatar.com/avatar/" . md5( strtolower( trim( $this->email ) ) ) . "?d=mp";
}
return $image;
}
}
Upvotes: 1