Creating Assignment Model 0:00At this point, you know how to create an Eloquent model. Here's an example of one. And you also know how to write basic queries using the API. However, what other logic would we put in a class like this? Let me give you an example. Let's imagine we are working with assignments. All right. I will create a new model called assignment. And as part of that, like we learned in the last episode, I want a migration and a controller. All right. Designing Migration Columns 0:23And as part of that, like we learned in the last episode, I want a migration and a controller. All right. Let's open up this file here. Create assignments table. And yeah, imagine this is something a manager or a team owner could assign to their staff, maybe lessons, maybe tasks, whatever it happens to be. For now, maybe an assignment has a body. An assignment would also likely have a due date. So let's add a timestamp for due date. And that can be null.So let's add a timestamp for due date. And that can be null. And lastly, an assignment can be completed. Now, as it turns out, you could write this in two ways. We could do a simple Boolean, zero or one. However, you might also consider making this a timestamp itself and then changing it to completed at, which can be nullable. Okay. So what are the pros and cons? Well, think about it.So what are the pros and cons? Well, think about it. If I come back to the Boolean approach, we effectively have zero or one. If it's one, it's completed. If it's zero, it's not completed. However, if we choose a timestamp, well, if the field is null, it's falsy, which means it has not been completed. However, if it's a timestamp, not only do we know that it was completed, but we also know the exact moment it was completed. And that can be valuable.but we also know the exact moment it was completed. And that can be valuable. Anyway, something to think about. For now, I'm going to keep it super simple and stick with the Boolean. So now I will migrate my database. And finally, if I switch to TablePlus, you can view the new table here. Now you'll remember that not only did we create a migration, but we also created an assignment model as well as the controller. Let's use this model to create an example record. And we're going to use PHP Artisan Tinker for this. Seeding Data with Tinker 2:04Let's use this model to create an example record. And we're going to use PHP Artisan Tinker for this. Think of this as a PHP and Laravel playground. You can run basic commands. If you need to practice a function call or resolve something out of the container, you have access to everything here. I use it all the time. Anyhow, I'm going to begin by building up a new assignment. So remember the namespace is app. So we would say new app assignment.So remember the namespace is app. So we would say new app assignment. Okay, next, what fields do we need? Well, we already know this. We defined it. Id, createdAt, and updatedAt will be automatically generated. So we only need for now body and completed. However, I just noticed a little mistake. We set a completed field. And if you think about it, by default, when I make a new assignment, is it completed? Adding Column Default 2:50We set a completed field. And if you think about it, by default, when I make a new assignment, is it completed? No. It will always begin as incomplete. So we need to make sure we set a column default. So again, this is where migrations are incredibly useful. We switch back. I will set a default of false. And then we rerun it. I'll hit Ctrl C to exit out.And then we rerun it. I'll hit Ctrl C to exit out. phbArtisan.migrate.rollback, the last run. And then run it again. Great. So now, if I give that a refresh, we fixed the mistake. And we do have a default here. Okay, let's bring back phbArtisan.tinker. I can hit up to return to my previous commands. And we can set the body.I can hit up to return to my previous commands. And we can set the body. And you know what? I think that's all I need for now. So assignment save. We've now persisted it to the database. If I switch back here to data, give it a refresh. Sure enough, we have our new assignment. Now, we can fetch all of these from the database by saying app assignment all. Give me everything from that table.Now, we can fetch all of these from the database by saying app assignment all. Give me everything from that table. And we get one there. Or I could say, give me the first one. Or I could even say, give me all assignments that are not complete. Where completed is false. And a little gotcha here. Even though you can say all to fetch everything from the database, once you're constructing a full query, that won't work. From that point forward, just say get.once you're constructing a full query, that won't work. From that point forward, just say get. And there you go. And in this case, we get the same record. However, we could change it. Give me all completed assignments. And in this case, we get nothing. However, let's imagine. Let's come back. I'll hit up a few times. Encapsulating Business Logic 4:33Let's come back. I'll hit up a few times. And I'm going to grab that first assignment and then save it to a variable. Let's imagine the student works on the assignment and they have completed it. Well, how could we complete this in the most readable way? And not just readable, but in a way that makes sense for the business. That uses the jargon and the lingo of the business. Now, in our case, it's going to be very simple. But you can imagine so many businesses with their own sets of terminology and the way they go about things.But you can imagine so many businesses with their own sets of terminology and the way they go about things. When you can, make the code reflect the way that you would normally speak about the business. So in this case, we might say a user can complete an assignment. All right. Well, assignment. And we have complete. Well, maybe complete is a method for an assignment. In that case, we'd have something like this. Assignment complete.In that case, we'd have something like this. Assignment complete. It matches. However, if I run it, well, that method doesn't exist. Let's come back to our editor. I will now return to the assignment model. And we have our first method, complete. Now, what do we put here? How do we go about completing an assignment? Well, it sounds like we need to update this field to one.How do we go about completing an assignment? Well, it sounds like we need to update this field to one. Here's how we can do that. I have my current assignment. So I will update the completed at column to true. And then I will save it. All right. Let's give it a shot. I will hit control C to exit out and then open it once again, so that it takes into account the changes I've made to the code.I will hit control C to exit out and then open it once again, so that it takes into account the changes I've made to the code. Next, I'll hit up a couple more times to fetch that assignment. And once again, I will call complete. Done. If I switch to TablePlus and I refresh, sure enough, we've now completed that assignment. So now at this point, you might be thinking to yourself, well, why did I need to create a method when instead, I could simply have done this directly? Why Use Model Methods 6:24well, why did I need to create a method when instead, I could simply have done this directly? For example, we could have said assignment completed is true here and assignment save, and everything still would have worked. So why do I need to wrap this within a method? And the answer is, well, like many things in programming, it just depends. However, what you have here, these two lines, it's very procedural. It's step oriented. Here are the steps in order to mark an assignment as complete. And it's fairly simple at the moment.Here are the steps in order to mark an assignment as complete. And it's fairly simple at the moment. But you can imagine that in real life, a number of things need to take place in order to mark an assignment as complete. Well, are you going to repeat that code all over the code base every single time you want to complete an assignment? No, we want to expose a clean API that anyone can use. And they don't have to think about all the various steps that take place when they complete an assignment. Instead, you have your assignment, call complete, and you're done.when they complete an assignment. Instead, you have your assignment, call complete, and you're done. Now, if that's still a little confusing, it's okay, don't worry. We will continue chipping away at this idea throughout the entire series.