Creating Category Model0:00
Okay, let's take a look at categories here. So categories are associated with ideas. So let's go ahead and generate a new model, php artisan make:model Category, and let's generate everything. Okay, so let's think of the relationship between ideas and categories. So can an Idea have many Categorys? Sometimes it can, but in this case, we only have one Category per Idea. So Idea belongs to Category, and a Category can have many Ideas. So let's start with that. So Idea belongs to Category.
Defining Eloquent Relationships0:34
So let's start with that. So Idea belongs to Category. So let's add that down here. So let's say category, and let's return this belongs to category, Category class, okay? And for categories, let's make a method here, say ideas, and return this hasMany Idea, okay? Next let's work on the migration. So a Category, for now, let's just give it a name. So table, string is fine, name, okay? And for the factory, we don't really need this because I'm going to hard code the categories
Updating Migrations and Factory1:24
So table, string is fine, name, okay? And for the factory, we don't really need this because I'm going to hard code the categories in, but let's add it anyways. Just say name, and let's just give it some words here using faker. So this faker words, let's say two words, and as text, okay? Oh yeah, and we also have to change the migration for ideas. We need to add a category_id column. So that would be this one. So let's put it after the user_id. We can just say category_id, and Laravel will automatically detect that it's looking for
Fixing Migration and Seeding Errors2:03
So let's put it after the user_id. We can just say category_id, and Laravel will automatically detect that it's looking for a categories table. So let's go ahead and try that out. php artisan migrate:fresh --seed, and we get an error. So if you look at the error message, it says fail to open reference table categories, and that's because the order of our migrations. So we get here, the ideas table is looking for a categories table, but at this point it doesn't exist yet. So what we have to do is move this before this.
it doesn't exist yet. So what we have to do is move this before this. So the easiest way to do that is just to rename this. So it goes before this. So I'm just going to grab this name here, and let's grab the timestamp here, and I'll just change the date by one day before. I'm actually not sure if there's an easier way to do this. So if there is, please let me know in the comments. So let's rename this one, and let's just say one day before, and let's say 25, okay? So now it appears before, and we might have to clear our cache, and maybe even do a composer
So let's rename this one, and let's just say one day before, and let's say 25, okay? So now it appears before, and we might have to clear our cache, and maybe even do a composer dump-autoload for this to recognize that it's changed. So let me just do that just in case, and let's try again. php artisan migrate:fresh --seed. Okay, we're still getting the error. Actually we're getting a different error now. It's saying category_id doesn't have a default value. That's because in our IdeaFactory here, from the DatabaseSeeder, we need to specify a category_id here as well.
That's because in our IdeaFactory here, from the databaseSeeder, we need to specify a categoryId here as well. So let's say categoryId, and I'm just going to make it a random integer between one and four, and we'll just hard code four categories in the seeder. So we'll do this figure, let's say numberBetween(one, four), and we'll make four categories in our seeder here. So I'll just paste that in here. So we have four categories, one, two, three, and four. Let me just import Category, okay? And now let's try that again.
Let me just import Category, okay? And now let's try that again. php artisan migrate:fresh --seed, and what do we get now? So I believe I have to create the categories before we create the Idea. So let's move this up, let's get rid of this, and let's try that again. php artisan migrate:fresh --seed. Okay. So now in our database, we should have those four categories. Let's refresh this. There it is, categories four, and our ideas are associated with a random category here.
Displaying Categories in Views4:36
Let's refresh this. There it is, categories four, and our ideas are associated with a random category here. So now in the browser, let's make sure we are showing the correct category here instead of hard coding it. So let's go to index, I believe, and category one is what's hard coded. So we can just do idea, category, name, okay? And that should show the category, it does, okay? And we can do the same for when we click in on the show view. So it's the same thing. Let me just grab this, show, and let's look for category.
Resolving N+1 Queries5:19
So it's the same thing. Let me just grab this, show, and let's look for category. There it is. Let's paste that in, and that should work as well, and it does, cool. So we have categories here as well in the createIdea, but we'll get to that when we work on that feature. And also in the filters here, and again, we'll work on that when we get there. So if you look down here at the bug bar, you'll see that we have 21 queries running, and that to me looks like an N plus one problem. So if we open it up and we take a look at the queries running, you'll see that we have
to me looks like an N plus one problem. So if we open it up and we take a look at the queries running, you'll see that we have categories and users here running for each one of these ideas that show up. So to fix that, we should be eager loading the relationships for the Idea. So let's go to our IdeaController, and right now we are not eager loading those relationships, so let's do that here. So let's say with, so we want to eager load the user relationship, and also the category that we just added, and that should reduce the number of queries. And we put this on its own line, and let's see if the query count is lower now. Okay.
Updating Tests for Categories6:30
And we put this on its own line, and let's see if the query count is lower now. Okay. And it's down to three. Okay. Now let's update our tests. So the same test we did for showing the Ideas, I just want to make sure that it shows the Categorys now. So that should be in, I forgot what I named it. Was it ViewIdeasTest or ShowIdeasTest? So it's ShowIdeasTest.
Was it viewIdeasTest or showIdeasTest? So it's showIdeasTest. And now we have to create categories and associate them with the ideas. So let's create two categories here. Category one. Actually, this is the wrong test. Let's start with the one up here. List of ideas show up on main page. Okay. Say Category one, let's use the factory Category factory create, and let's just say name is
Okay. Say category one, let's use the factory factory, create, and let's just say name is category one. Okay. And let's make another one as well. category two, category two, and let's make sure to import Category. Okay. Now we have to associate the ideas with these categories. So let's say category_id is category_one_id. And let's do the same for idea two.
So let's say categoryId is categoryOneId. And let's do the same for ideaTwo. Okay. Now we just have to assert that we see the text here. So let's add that here. title, description. Let's add, we can do categoryOneName, so categoryOneName. Okay. And we can do the same for the second category as well. Okay.
And we can do the same for the second category as well. Okay. So let's see if this test passes. So run the test and it does pass. Let's make sure that that works correctly. Let's go to our index.blade.php. If we remove this, the test should fail now. And it does. Cool. So let's put that back and we can add the same for the show page here.
Cool. So let's put that back and we can add the same for the show page here. Single Idea shows correctly on the show page. So it's basically the same thing. We just have to grab, I think we only need one Category here since it's only showing one Idea. Let's do that. And let's add a categoryId here. Okay. And it's the same thing.
Okay. And it's the same thing. categoryOneId. And we can assert that we see, we just grab this one here. And it should be good. So let's run that test again. And it does pass. And I believe these tests are going to fail now because there's no Category associated with the Idea. So if we run that, yeah, it does fail.
with the Idea. So if we run that, yeah, it does fail. So we have to create a Category and just force it to that Category. So let's grab this. And for create, let's just say categoryId is that Category, or you can just force it to one. Okay. And let's run that. Okay. It's passing again.
Okay. It's passing again. And same for the last test here. Should fail. Okay. Let's paste in that category and let's force it here. categoryId. categoryOneId. Okay. Does it pass?
Okay. Does it pass? Does not. Oh, I have to do it for the second Idea as well. So let's force it to the same category. Run the test again. And it does pass now. So let's run all of our tests to make sure everything is working correctly. And it does. So let's go ahead and make a new commit here.
Committing Category Changes10:50
And it does. So let's go ahead and make a new commit here. So what do we have here? This is episode 13. So git add, git commit. Episode 13, add categories.
