hit counter

Easy way of grabbing a user's Gravatar image in Laravel

Posted on April 19th, 2018.

When you're creating a public Laravel application, you may want users to have profile images. There are a few ways of handeling profile images.

You can either let users upload images to your server or you can let Gravatar do it all for you.

The Gravatar solution is the easiest to implement. I followed this stack overflow article and found the first response really helpful.

All you need to do is to add the following to your user model.

public function getGravatarAttribute()
{
    $hash = md5(strtolower(trim($this->attributes['email'])));
    return "http://www.gravatar.com/avatar/$hash";
}

Then you can add an image tag where-ever you want the gravatar image to show. Like this:

<img src="{{ Auth::user()->gravatar }}">

It's really simple! This solution made my day :)