In this episode, we improve our post display by adding more relevant user data and tidying up our frontend code.
First, we clean up some of the unnecessary markup in our components to keep things neat. Then, we tackle the problem of how to safely expose public user information with each post—without accidentally leaking anything private, like email addresses. For this, we create a new PublicUserResource
that only includes safe fields like the user ID, username, and (soon) their avatar URL.
Next, we go ahead and implement an avatar by leveraging Gravatar, generating the avatar URL from the user's email (hashed, of course). This way, each post can display a little user icon next to the username.
From a UI perspective, we arrange things to show the avatar on the left, the username and the post's created time next to it, and then the actual post content underneath.
Another nice improvement is around formatting timestamps. Instead of scattering date formatting logic all over the place, we create a DateTimeResource
to consistently format timestamps (like '5 minutes ago' and full datetime strings) in one reusable spot, just passing Carbon instances through it.
By the end of the episode, each post shows the avatar, username, a nicely formatted posted time, and the post body—all with cleaner code and better organization!