Export Badge SVGs0:33
Now if I click on one of these, it looks like I don't even have an SVG I can download, it's just basic shapes. And that's okay, we don't have real badges yet, we'll just stick with a blue circle and a red circle, with the understanding that when I get to it we will substitute those with actual real icons that I will show you later in this series. So we're going to save, as silly as it is, this blue circle here, we could do it with a S, but we're representing some kind of icon. And maybe this will correspond to the LaraCast Mastery example achievement that we added. So I will save that, and you'll see within my images directory I do have this badges folder.
So I will save that, and you'll see within my images directory I do have this badges folder. So we will export that, and then once again this one will represent a different one. Don't we have one called first thousand points? And save it. So now if we open that directory, you can see two SVG files. Great. If I now switch back to my editor, let's go to first thousand points, and you can see here it's referencing an icon called some badge. We're going to update that.
Update Achievement Icon Names1:27
here it's referencing an icon called someBadge. We're going to update that. And you know what? We're starting to see that often we name the badge the same thing as we name the class. So that might be a good example of once again using a sensible default, like we did with the achievementType name. Maybe we'll come back to that later. Until then, I will hard code it. Okay, and this one is already LarakastMastery. Now you'll remember as part of the instantiation, what we do, I'm sorry, on the abstract class
Mapping Classes to Database1:47
Okay, and this one is already Larikast Mastery. Now you'll remember as part of the instantiation, what we do, I'm sorry, on the abstract class at the moment is we are ensuring that an achievement of this type exists within the database. It's almost like we're creating a direct mapping between a record in the database and a specific class. Now we might want to do some checking in the future where we say, well, if any of those fields have changed, we need to update that record in the database. Because at the moment it's going to create a new badge entirely. Anyways, that's at least something to consider. Now I'm going to go to my routes file, and I want a simple place where we can just experiment.
Create Test Profile Route2:18
Anyways, that's at least something to consider. Now I'm going to go to my routes file, and I want a simple place where we can just experiment with this. I don't need you to see the controllers I'm working with or the views. It's just going to confuse you with a bunch of stuff that isn't related to this lesson. So for the purposes of this lesson, let's just register something like User profile. Yeah, and then later I would actually put that under a profiles namespace. Okay. So what do we need to do here? Well, the first step is to fetch the user's achievement badges.
Fetch User Achievements2:43
So what do we need to do here? Well, the first step is to fetch the user's achievement badges. So I could say auth()->user(), and we've learned that we have this relationship called achievements. So if I return that, let's view it in the browser. If we run this, we get an empty collection. Let's fix that. I'm going to open up php artisan tinker and award myself some experience. I will find myself, and then I will get the experience and award experience 2000 points. Again we're simulating the user doing various amounts of activity on the site. We're just going to give them a bulk 2000 points.
Award Experience via Tinker3:18
Again we're simulating the user doing various amounts of activity on the site. We're just going to give them a bulk 2,000 points. Now you'll remember before I execute that, as part of experience, award experience, we fire this userEarnedExperience event. Then in our AchievementServiceProvider, we then listen for that event, and then we filter through all of the achievements, and we conditionally apply them based upon a qualifier, and we set each of these here. So here's the qualifier for this achievement, and here's the qualifier. Again, these are kind of generic, but you get the idea. Okay.
So now if we switch back to Chrome, here we go. At this point I have 2,000 points, and I qualify for the first achievement badge. So now we can filter through all of the achievements, render their associated badges on the page, and then for each one we do a quick check. Has the user earned this achievement? If so, maybe apply a CSS class, maybe apply some text. And then move on to the next one, and we'll tackle that very thing in the next episode.
