This episode is for members only

Sign up to access "Nested Categories and Breadcrumbs with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
03. Seeding nested data

Transcript

00:00
When we created our model, we created the category seeder, which we're going to use to just initially hydrate the database with a bunch of data. A little bit later on, you can apply this to whatever admin panel you're using to manually create categories. But for now, we just want a bunch of categories created out. So let's start with just the root category.
00:19
So we're going to say app models category. We're going to grab the factory for this, and let's go ahead and create this with some data. So we'll create this out and let's think about what we want to create. So we just need a title here and let's just say shoes and we'll apply the slug in here as well.
00:37
We'll be doing everything really manually here rather than iterating over, but you can change this up if you need to as well. Let's go ahead and run our seeder. So let's say PHP artisan DB seed and we'll seed our category seeder. That's going to go ahead and create that in the database like this.
00:52
So we just have a single category out here with shoes, parent ideas, now left and right is zero. Before we start playing around with this, let's make sure that we're always deleting this at the top. So to do this, we're going to say category and query, and then we're just going to delete. That's just going to remove everything before we start to seed to make it a lot easier.
01:11
Now, before we go any further, when we're working with this package, if we head over to the category model, we need to apply a trait in here, which will set up all of the relationships that we need. So we'll have ancestors, children as part of all of our categories. Let's go ahead and use the node trait from this package, and that's going to give us all of them relationships. Let's just take a look inside of here really quickly.
01:34
And if we go down, you can see we've got a children relationship. We've got ancestors, we've got. Descendants, all of that good stuff that we can use to iterate through our categories, grab the children, grab any ancestors, which we're going to use for our breadcrumbs. But now we need to figure out inside of our seeder, how we actually store this data.
01:56
So what we can do is create a child using has. So with a factory, we can use has, and then we can create out another category. So again, we're going to grab out a factory instance for our category, and we're going to change the state here. Now state is going to be similar to create.
02:13
We just need to pass in the title of this that comes underneath here. So let's go ahead and say boots, and then we'll go ahead and set the slug here to boots. What this will do is it will create out the main category, and this category has a subcategory, but if we run this on its own, it's not quite going to work. Let's go ahead and just try this out quickly.
02:33
Okay. So the error we get here is that we have an undefined method, category, category. By default, what this is doing is looking for a relationship called category. That's not the case.
02:44
So there's a second argument to has, we can actually pass in the relationship. For this, it's going to be children. So we know that we want to set this as a child to this overall category. Let's go ahead and run that again and see what happens.
02:57
And sure enough, you can see it worked. Let's go over to the database. And now you can see, ignoring the left and right columns, this now has a parent ID of three. So we know that boots comes under this shoes category.
03:08
Okay. So now that we've done that, let's go ahead and just create a bunch more, just so we have enough data in here to play around with. So again, what we can do is just duplicate this down and create another subcategory under here.
03:19
So let's go ahead and call this formal and create formal out here. And again, we use the same relationship. We can take this even further by creating subcategories of subcategories by just chaining onto the category that we have just here. So we could say that this has another subcategory.
03:36
So let's go ahead and create another category under each of these. So let's create out a category factory instance again, and go ahead and set the state. So this is underneath the boot subcategory. Now let's create a title in here called new in, and we'll set a slug here as well.
03:53
Now we need to be careful because each of the slugs in the database has to be unique. So we're going to just change around the slug. We're going to look at the end of the course at how we slightly change around how the URLs work. So we could potentially get rid of this unique stuff, but let's go ahead and do this.
04:09
Anyway, so we're just going to say shoes, boots and you in and let's create another one as well. Remember, this still needs to have a children relationship. So let's add another one in here underneath this subcategory called sale. And let's go ahead and say shoes, boots and sale, and we should be good.
04:29
Let's go ahead and run this again. And we'll keep running this just to make sure the data is good. And there we go. You can see all of them parent IDs nicely match up.
04:37
Okay. Let's go back over and create another root category. So we're just going to grab everything that we've seeded in here. And we're just going to duplicate this down at the very bottom.
04:46
And of course you can add more slightly change around the way that this works if you want to. Okay. So we are going to create out a jackets overall category. And then remember, this is the subcategory.
05:01
So again, we could say outdoor, change this to outdoor. And we could also use new in. So let's say jackets and outdoor new in and jackets, outdoor sale. And let's go ahead and change this one up to winter, for example, and we'll
05:21
change this to jackets and winter again, do whatever you want here. Okay. Let's go ahead and run this again and see what we get. And there we go.
05:28
Right. So I think we've got enough data to play around with here. This should give us a good example of all of the nested stuff. Okay.
05:36
So I think we've got enough data here to play around with. Let's go ahead and look at how we grab the root categories over here. So if we head over to the category index controller, we're going to pass down all of the root categories.
05:50
So let's say categories and we'll grab our category. Now, if we were to just say, get an iterate over this, this would iterate over every single category. Let's do that first of all.
06:00
And then we'll take a look at just grabbing the root ones. So if we head over to categories and index, let's start to iterate over each of these. So we'll say for each categories as category.
06:13
And let's go and just output this in a div. We'll create an anchor cause we'll need to be able to click through there and let's say category and title. Okay.
06:22
If we head back over and give that a refresh, there we go. We get every single category in here. And what we want to do, at least initially, later, we're going to look at building a tree of this data and recursively nest
06:33
through using a blade component. Let's keep it simple for now. And let's go and update our category index controller just to grab the root categories.
06:41
To do this, we have some built in scopes with this package. So we can say, where is root? That will only grab the top most categories or top most items you're iterating over.
06:51
Let's give that a refresh. And there we go. Great. So we've got shoes and jackets, which are our two main categories.
9 episodes 46 mins

Overview

Let’s master creating nested categories and displaying breadcrumbs for easy navigation.

We’ll start by setting up and seeding nested categories, recursivly iterating over and displaying them with a nested Blade component, then render and customise breadcrumbs to show a trail of category ancestors.

Finally, we’ll finish up with looking at using wildcard routes to show each category slug for perfectly clean URLs.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!