This episode is for members only

Sign up to access "Build a Livewire Comment System" right now.

Get started
Already a member? Sign in to continue
Playing
15. Adding simple avatars

Transcript

00:00
Let's quickly add avatars from an external source. And why don't we fix up this comment count that we forgot to add while we're here.
00:07
So let's work on that comment count first of all. Okay. So over in our main comments component, just down here, we just want to go ahead and output the count of all the comments, which is pretty straightforward.
00:19
So we just want to say comments and count, and we should be done. There we go. We have six parent comments here. Okay.
00:26
So for avatars, let's head over to the user model and let's add in a helper for this. So let's come down here and we'll go ahead and create our own avatar helper from here. It's really up to you where you pull this from. I'm going to go ahead and pull it from Gravatar.
00:43
So what we need to do to get this to work is pull in the Gravatar URL. Then at the end of this, we want to hash the email address. So we're going to do this with SHA-256 and we want to do a string to lower on the email address.
00:59
So let's grab the user's email address and we could also trim this as well. I think that kind of makes sense just in case there's any additional white space. And that's pretty much it. That's a really simple avatar URL based on a service where the user can sign up
01:15
for, give their email address and upload a profile photo. So let's go ahead and output it within our comments item. So if we look for the image that we had earlier, all we need to do is we could provide an alt in here of the comment user name, and we just want to output
01:33
the comment user avatar and we should be good. There we go. So you can see my comments are with my photo and we have the default here. We can adjust the defaults if we want to.
01:45
So for example, we could add onto here, let's pull this down, a default here of retro, for example, and that's going to go ahead and change that up. Of course you can use any service for this you want, but Gravatar is usually the most popular.

Episode summary

In this episode, we start off by fixing a small thing we missed before—actually showing the total comment count in our main comments component. That's just a quick update, simply outputting the count of comments that already exist, so nothing too fancy there.

Then, we move on to adding user avatars to our comments. For this, we dive into the user model and create an avatar helper that generates a URL for the user's avatar. We're using Gravatar for this, which means we need to hash the user's email address (after trimming and lowering the case) and use that hash to construct the avatar URL. If the user has a Gravatar account, their chosen image will show up—otherwise you'll see the default style (which we can change, as shown in the video).

With the avatar helper ready, we head back into our comment component and simply update the image tag to use the avatar helper as the source, using the comment user's name as the alt text. Now, each comment is personalized with its author's avatar. You can tweak or use any service you like, but Gravatar is a popular and really easy choice!

Episode discussion

No comments, yet. Be the first!