Introducing Eloquent Models 0:00Let's talk about Eloquent. I think you'll like this. I'm going to make a model here. And we've been working with tasks just a little bit longer. So, a model can be the representation of something in your system. It could be a noun, but it doesn't have to be. It could be a number of things. In our case, we want the representation for a task. Now, you'll see that's stored within the app directory. Task. Okay, so we can see that this extends Laravel's Eloquent model. Querying in Tinker 0:23Task. Okay, so we can see that this extends Laravel's Eloquent model. Now, Eloquent is Laravel's active record implementation. And I don't even expect you to understand why that matters. The key thing, though, is that this simplifies much of your interaction. For example, I'm going to run phpArtisanTinker, and this will boot up Laravel's shell. It gives us a nice way to interact with the application, sort of like tinkering around. Now, we've created a task model, and it's been namespaced app,sort of like tinkering around. Now, we've created a task model, and it's been namespaced app, which means I can reference it by saying app task. Now, simply by extending Eloquent, we gain access to a number of methods for querying and working with the database. For example, if I want to fetch all tasks, I can run this command. And in this case, I have three. But what if I only want to fetch the tasks that have an ID of 2 or greater? Okay, I'm going to hit Command-K to clear the screen, and then up to cycle to my previous command.Okay, I'm going to hit Command-K to clear the screen, and then up to cycle to my previous command. So it sounds like we want to say where the ID is greater than 2, and then get me all of the results. But in our case, we actually said greater than 1. So we could do greater than or equal to 2, or just greater than 1. And now we should get two records. Done. Or maybe we just want to fetch the body from every task. App task.Or maybe we just want to fetch the body from every task. App task. And I'm going to pluck the body from every record. Now I get a collection that contains exactly three items. So we'll talk more about collections. They're really great in Laravel. But they give us access to a number of helper methods for working with a collection, like the very first one. So now that we have this, yeah, let's try it out. I'm going to go back to routes, web. Swapping Routes to Eloquent 2:00So now that we have this, yeah, let's try it out. I'm going to go back to routes, web. And right down here, this time we'll say tasks equals app task all. So if we now come back and refresh, we get the exact same thing, but we've switched over from the query builder, like this, to using Eloquent in a dedicated class. And what's especially nice about this is you can add any number of methods to add additional behavior. So, for example, some behavior that a task might expose is whether or not it has been completed. Adding Model Behavior Methods 2:28So, for example, some behavior that a task might expose is whether or not it has been completed. So maybe you have a method called isComplete. Let's just hard code a result and we'll say false. Okay, so now let's go back to Tinker. We're going to find the very first task. So I will say task. And we could say find1 to find the ID of 1. Or I could even say give me the first record. And as you can see, it's an instance of task,Or I could even say give me the first record. And as you can see, it's an instance of task, which means I can call any methods on here. Task is complete. And, of course, that returns false. Now, when you're building your applications and you're talking about specific things like this, try to pay close attention to the terminology that you or the client or your company uses. For example, if you were building a little project management task tool,or the client or your company uses. For example, if you were building a little project management task tool, maybe your client or the people you discuss it with refer to archiving a task. So when the user is done, they can archive the task. Well, if that's the term they use, then do your best to represent that within the code by having an archive method. And now here, you might perform a database query or something along those lines.And now here, you might perform a database query or something along those lines. Anyways, we'll talk about that quite a bit more in future episodes. So let's switch back and move this over to Eloquent as well. Task find by the ID. So back to Chrome, click on one of these, and we get class task not found. Whoops, that's because, remember, any of these classes will be namespaced at the top. So think of it as a directory structure.any of these classes will be namespaced at the top. So think of it as a directory structure. You're trying to reference task, but you didn't specify that task was in a namespace or a subfolder. So we always want to do this, like so. All right, come back and refresh, and there we go. Task with an ID of 3, 2, and 1. Or, when it comes to namespacing, what you can always do is use it at the top. So I want to import the task class, Generating Model with Migration 4:15what you can always do is use it at the top. So I want to import the task class, and now I can reference it as I did before. Refresh, and we'll still get the exact same thing. Now, let me show you a shortcut. In the last two episodes, we created a migration and then created our Eloquent model. But we can actually do those at the same time. So let's do this. We're going to run php artisan migrate reset.So let's do this. We're going to run php artisan migrate reset. We're going to roll everything back, and let's just delete our task model entirely and start from scratch. The entire thing we're going to reset. Okay, here's how I would recommend doing it. You decide that you need a task, but you also need a tasks table. So you can say php artisan make model task,but you also need a tasks table. So you can say php artisan make model task, but we can also add this dash m flag. And let's take a look at what that does using help. If you add the dash m flag, this specifies that we also want to create a migration in the process. And you'll see there's also one for dash c to create a controller in the process. We haven't yet talked about controllers, but we'll do that probably in the next episode.We haven't yet talked about controllers, but we'll do that probably in the next episode. So let's do this. Make a model called task, and I do want a migration file. Now once again, we're getting that same thing, and that's because the way Composer works is for autoloading in some situations, it basically stores all of the files that should be autoloaded. But now in this case, it can't find it.it basically stores all of the files that should be autoloaded. But now in this case, it can't find it. So what we want to tell it is because I deleted that file, just refresh. So dump all of our autoload files and refresh them. Okay, one more time. I'll press up a couple times. There we go. So you'll see that yes, we have a migration, but it also whipped up our task model in the process,So you'll see that yes, we have a migration, but it also whipped up our task model in the process, which is great. Okay, so now, yeah, just as you did before, you might say we'll have a string or really a text field so that they can type more. And while we're at it, why don't we add one more section that will be a boolean that indicates if the task is complete, maybe completed. Okay, so we're ready to migrate the database,that indicates if the task is complete, maybe completed. Okay, so we're ready to migrate the database, and we're all set. Now, I'm going to whip up a couple tasks directly within Tynker. Here's how. I can say create a new task, and now we're going to populate each of these. So the task body will be go to the store. The task completed.So the task body will be go to the store. The task completed. Well, if you think about it, every new task will not be complete, right? So it seems weird that I have to be explicit because, of course, it's not complete. Okay, let's set a default with that in mind. Default will be false. Okay, so now we've modified our migration. I'm just going to refresh all of themOkay, so now we've modified our migration. I'm just going to refresh all of them so that it takes effect. Boot up PhpArtist in Tynker, and then try again. At task, task body, go to the market, and task save. So now, if we switch over to SQL Pro, give it a refresh, you'll see that, yes, completed defaulted to false,give it a refresh, you'll see that, yes, completed defaulted to false, or zero versus one. Now, let's find all tasks again, or maybe the first one. Yep, and we'll say completed is zero. Let's add one more to be quick. Finish, screencast. Okay, so if we switch back to Chrome now and give it a refresh,Okay, so if we switch back to Chrome now and give it a refresh, we have our two new items, and that's a good workflow to get into. You create a model, phpArtisan make model. You give it your name, and generally this will be singular. So if I'm creating a user, well, then the model is called user.So if I'm creating a user, well, then the model is called user. If I'm creating a post, my model is post, the table is posts. If I'm creating a notification, then the model might be called notification, and the table would be called notifications. But you can see it's not limited to that. So as you can see here,But you can see it's not limited to that. So as you can see here, password resets, entirely fine. And you can even set up things like pivot tables to store relationships between two or more different tables. So think about what we can do now with what you've learned. Let's create one last one. Learn Laravel. Creating Query Scopes 8:14Let's create one last one. Learn Laravel. Okay, let's imagine that we have gone to the market, but that's the only one that we've completed. And now we want some way to fetch all tasks, but only those that have or have not been completed. Okay, like this. phpArtisanTinkerOkay, like this. phpArtisanTinker app task where completed is zero, and then give me the results. So now you'll see that we get only the tasks that have not been completed. Or, subsequently, if we do one, this will give me only the tasks that have been completed.this will give me only the tasks that have been completed. So that's great. But you know what? We could even make this cleaner. Because right now, this is a long query, and it works great. But what exactly does this represent? Well, this represents completed tasks. And the other one represented incomplete tasks.Well, this represents completed tasks. And the other one represented incomplete tasks. So wouldn't it be nice if we could add a way let's go back to our routes web file to do something along the lines of task incomplete. That could be useful, right? Or maybe even something as simple as this. Here's how. These are called query scopes,Here's how. These are called query scopes, and we can add them to our task model. Now you have a couple options. One, you could create a static function called incomplete, and this will just be a wrapper around that query. Like this. Return task. Well, we're within the task model,Return task. Well, we're within the task model, so let's just reference it as static. Static where completed is zero, and then give me all the results. Okay, so we're taking a basic query and wrapping it within a method. So if we give that another shot now, at task incomplete, now we get those records.at task incomplete, now we get those records. So it's the exact same result, but we've added much more clarity as to what that query actually does. And even further, we've wrapped that query within a method that describes exactly what it does. However, at the same time, you may decide that you need to continue chaining.However, at the same time, you may decide that you need to continue chaining. So for example, give me all of the tasks that are incomplete, but also those that were created a month ago. Well, in this case, we can't do that. But if we instead use query scopes, we can. We can do it like this. We make it a regular public method, and then we use scope,We make it a regular public method, and then we use scope, and then how we want to reference it. Incomplete. So this allows us to do task incomplete. However, because we prefix it with scope, Laravel knows to treat this as a query scope. So it will accept the existing query, like so. And if you need to pass anything to it, then you can do that here.And if you need to pass anything to it, then you can do that here. So that allows you to do task incomplete. But if you need to pass some data to it, not overly appropriate here. But if you did, you could pass it through as the second or third arguments. Okay, now we're going to say return query where completed is zero. So yeah, a query scope is just a wrapperwhere completed is zero. So yeah, a query scope is just a wrapper around a particular query. Now, if we give it a shot, and we say task incomplete, you'll see that it returns an instance of the Eloquent Builder. And that's because we haven't executed the query yet. So we don't have a collection as the result. We need to be explicit.So we don't have a collection as the result. We need to be explicit. And there we go. So like I said, now with this syntax, we could say incomplete, but also maybe where the ID is greater than or equal to two. Then give me the results. Great. So let's see this.Great. So let's see this. Let's say learn Laravel is complete. And we run it again. And by the way, whenever you make a change to your code, in this particular case, it didn't matter. But if you ever make a change to your code, sometimes you will want to Control C and restart it up.sometimes you will want to Control C and restart it up. And that way, it's working with the most recent changes. Anyways, now you'll see that we get exactly one record. This ID is two or greater, and it's incomplete. And it turns out that we have exactly one record that meets this query. All right, so good job. You're really beginning to learn a lot.All right, so good job. You're really beginning to learn a lot. I mean, if you think about it, you've learned some routing. You've learned a little bit about the Query Builder and switching over to Eloquent. You've begun learning about database migrations, which is great. You've learned about views, at least the beginning of views,You've learned about views, at least the beginning of views, and working with Blade. You've learned how to nest your views into folders that correspond to each thing that your application represents. In this case, we have tasks. So we store the index task with the show task. And on that note, we'll talk quite a bit more about naming conventions.And on that note, we'll talk quite a bit more about naming conventions. But yeah, you're well on your way. So really good job. In the next episode, we're going to talk about controllers.