How to Get Gravatar URL in Laravel Without Packages

In this gravatar Laravel example, I will show you from scratch that how to get a gravatar URL in Laravel. Let assume that we want to set a default image for a user and this situation gravatar is the best option I think. So if you don't know how to get a gravatar URL in Laravel then this example is for you.

So in this how to get gravatar URL in Laravel 8 tutorial, I will simply create a helper function and pass an email to get gravatar of that user. I won't use any packages to do that. We can use creativeorange/gravatar to get the gravatar URL but I won't use any package to get this gravatar url in Laravel.

Before gravatar lookup, we need to follow some steps. So let's start:

 

Step 1: Create Helper

In this step, we need a helper function where we will implement our logic to get the gravatar URL. So create it like below:

App\Helper\Gravatar.php

namespace App\Helper;

class Gravatar
{
    public function getUserGravatar(string $email) : string
    {
        $hash = md5(strtolower($email));
        return "https://www.gravatar.com/avatar/$hash";
    }
}

 

Step 2: Create Route

Now create a route like below to get gravatar URL in Laravel:

routes/web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\GravatarController;

Route::get('/', [GravatarController::class,'index']);

 

Step 3: Create Controller

Now in this final step, we need to create a controller and have to finish the index method to get the gravatar URL.

App\Http\Controllers\GravatarController.php

namespace App\Http\Controllers;

use App\Helper\Gravatar;
use Illuminate\Http\Request;

class GravatarController extends Controller
{
    public function index()
    {   
       return (new Gravatar())->getUserGravatar('enter_your_gravatar_account_email_here');
    }
}

 

Now if you visit this http://127.0.0.1:8000 URL then you will see the below output of my image URL:

 

gravatar output
https://www.gravatar.com/avatar/73dc35505c4018cd8beca47a2156657b

 

Read also: RSS Feed Example in Laravel Without Packages

 

Now hope you can use this code in your project to get a gravatar profile image in your Laravel application.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js