تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Recap Awarding Flow0:00

All right, I think it's about time to bring this series to a close. So let's do a final recap of the code, review what we've built, and then we'll call it a day. So throughout the last dozen videos or so, this is what we built. This is where we started, Award achievements. So we have an event listener that listens for, in this case, when a User earns experience points. And when they do, you can see behind the scenes I extracted a method here, but when a User earns experience, we scan for Awards and we sync it up within the database. Nice and simple.

Custom Collections for Achievements0:28

earns experience, we scan for awards and we sync it up within the database. Nice and simple. We also have things like this. When you fetch achievements from the database, we can return the results as a custom collection. This allows you to add additional methods. For example, if you want to scope all achievements to a given User or sort them according to a level. Here's another one I've added behind the scenes. We determine the percentage of achievements the user has completed. So if they have four achievements, but there's a total of eight achievements available, well

We determine the percentage of achievements the user has completed. So if they have four achievements, but there's a total of eight achievements available, well then that would return 50. That's a nice place to put logic like this. Now we registered that in our standard Achievement Eloquent model. So if we take a look, we override newCollection, and here is where we can specify when Eloquent is whipping up the collection to return your result set. Well, instead of the default collection class, let's use our own Achievements class that will extend from Collection. Next, any logic associated with an individual achievement, like if we want to translate

Achievement Model Responsibilities1:27

will extend from Collection. Next, any logic associated with an individual Achievement, like if we want to translate the skill level to a number for the purposes of sorting, we can put that there. If we want to award an Achievement to a User, this is another good place where we can store logic like that. Okay. What else? So we have our AchievementServiceProvider. This is where we take all of the different achievement types, and you can see all the ones that I've set up for Larakast, and these are the real achievements that you'll see

Registering and Caching Types1:48

This is where we take all of the different achievement types, and you can see all the ones that I've set up for Larikast, and these are the real achievements that you'll see on the site. Lots of good stuff here. So for each one of those types, we register it here, and then when we register the provider, we collect that array of achievements, and for each one, we instantiate it. And that's what allows us to sync up each of these achievement types with a record in the database. But we don't want to do that over and over, right? So we cache it indefinitely.

Artisan Achievement Generator2:16

But we don't want to do that over and over, right? So we cache it indefinitely. Very simple. All right. What else? Well, we have our generateAchievement command. This is a convenience we added that allows us to say php artisan make:achievement, and it will scaffold a new class within this types directory that matches something like this. Things like that are incredibly useful.

this. Things like that are incredibly useful. So you learned how to do that, and you can apply this to your own projects. So in our case, we figure out the path to the file we're creating. We compile down the template, and all we do here is we just fetch the stub that I showed you, and then we replace the class name with the name in the class you provided from the console. And then we put that to the file. Really really simple stuff, and it does save a lot of time. Now in terms of the various achievement types, let's take a look at one.

thing, but we're checking if you're at least five years. What else? How about pay it forward? Earned once you receive your first best reply award on the LaraCast forum. All right. That's a beginner. It's a basic achievement. And we're going to check that by looking at the User's experience, and I store a award_count column there, or field. And as long as that's more than zero, you qualify for it.

Testing Achievement Qualification3:46

count column there, or field. And as long as that's more than zero, you qualify for it. Now for things like this, of course it makes sense to have a set of tests to confirm that each of these works the way we would expect. Now that's something that is beyond the scope of this series, but I have done that behind the scenes. So if you'd like to take a look at those real quick, notice I have all of these different methods. One method per achievement. So let's go to oneYearMember.

One method per achievement. So let's go to oneYearMember. All right. Well, let's build up a User and let's just force their created_at timestamp to be a year ago. Then, if we sign in, I have this little helper method, seeAchievement. So if they meet the criteria, well, then I should see oneYearMember for that User. And if we want to take a look at that, you'll notice all I'm doing here is I instantiate AwardAchievements. So I instantiate this class here, and then I call that scanForAwards method and I pass

award achievements. So I instantiate this class here, and then I call that scanForAwards method and I pass it to the user. All right. So that means we will take that user, our test user, and we sync up all of the achievements. And notice right here, once again, we take those achievements, we filter through all of them, we call a qualifier method for each one that determines, well, does the user qualify for this achievement? And again, we sync that up within the database. So at that point, we get the user's achievements.

And again, we sync that up within the database. So at that point, we get the user's achievements. And let's just make sure we find an achievement with the given name. And you can see here I have one that does the opposite. I could even merge those if I want to remove some duplication, but no big deal. Anyways, once again, you'll see I have a test for each possible achievement. That way, if I deploy this and somebody says, hey, I'm pretty sure I should have this achievement, but it's not showing up, well, immediately I can go to the test and check, well, what's going on here? Is this working the way I expect?

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟