Alejandra Jaramillo
Alejandra Jaramillo

Reputation: 35

how can I show the user's image like a function on user model?

When I try to make a function to show the user's avatar I can not solved this error.

I need help! thanks

the image of error

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

Answers (1)

Christophe Hubert
Christophe Hubert

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

Related Questions