Laravel 5.1 Gravatar Plugin

Access all tutorials in sprocket icon.

June 10, 2015 from author Bill Keck.

Ok, now that I have my fresh install of Laravel 5.1, I’m going to do more tutorials on this blog.  For now, I’m going to start with something extremely simple, adding a Gravatar to your nav bar.

This is super simple thanks to the team at CreativeOrange. You can visit their packagist page for the original instructions, should this tutorial go out of date for any reason.

Step 1.

Grab the package through Composer via the command line:

composer require creativeorange/gravatar ~1.0

Or modify your composer.json to include:

"require": {
    "creativeorange/gravatar": "~1.0"

If you modify your composer.json file, you will need to run composer update from the command line to pull in the package.

Step 2.

Next we have to add the service provider to app/config/app.php in the providers array.


You’ll note we are using the PHP 5.6 syntax that is standard for Laravel 5.1’s providers. That’s just a single line of code to add.

Step 3.

Add the alias in the aliases array in the same app.php from above:

'Gravatar' => Creativeorange\Gravatar\Facades\Gravatar::class,

Again it’s just a single line of code. I really love this aspect of the Laravel architecture. Service Providers are the building blocks of your application and you can see how easy it is to make one visible to application. Just 2 lines of code in one file and you’re done.

Now your application can use the Gravatar class and you have a Gravatar facade, which makes pulling it in for use as simple as:

use Gravatar;

However for this implementation, we won’t even need to call it that way.

Step 4.

The simplest implementation would be to include it directly in a view, typically in your master page as a list item in your top nav. You would of course have to check first to see if the user is logged in. We do that via Auth::check() which is always available to us:

@if (Auth::check())

    //display the list item.


Obviously I’m using syntax from blade, which you should be familiar with if you are using Laravel. Blade’s intuitive syntax makes it easy to mix html and php:

@if (Auth::check())

<li> <img src="{{ Gravatar::get(Auth::user()->email) }}" style="height:50px; width:50px; border-radius:50%;"> </li>


Inside the list item, you can see it’s just a simple call to an image tag with our Gravatar class providing the proper image. The double brackets are blade syntax, so you don’t have to use php tags.

In the get method of Gravatar, we feed it the email of the current user, which is what we get from Auth::user()->email. I threw in an inline style to demonstrate making the image a circle, but that is completely optional.

If you need to adjust the size of your Gravatar, you can do it by modifying vendor/creativeorange/gravatar/config/gravatar.php.

'size'   => 80,

There are other settings you can play with in this file, such as fallback, secure, maximum rating, and forcing an extension such as jpg.

Thanks to CreativeOrange and Laravel for making this so simple. Have fun!

I hope you have enjoyed this tutorial and found it useful. Click on the sprocket icon at the top of the page to see all tutorials. Please comment, share, and like if you can, thanks!

I don’t have a donate button, but If you would like to support my work, you can do so by buying one of my 99¢ books, I really appreciate it.