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

Defining Taxonomies and Topics0:36

brainstorm a little bit. Alright, so if we think about it, of course we have Series, right? And a Series has a title, and a description, and a thumbnail, things like that. But now we want to categorize those, and we can use the term taxonomy for that. A taxonomy is a way of classifying something, right? So as we reviewed, these could be taxonomies. It's a way of classifying the Series, whether it's by framework, or the language used, or a package, or a tool, or a library. Those are taxonomies. So let's set that up.

Those are taxonomies. So let's set that up. And it's going to have a name, and maybe that's it. So let's use frameworks, libraries, language, again, different ways of categorizing a series. Okay, but now within each of those taxonomies, we'll have various topics, right? So if we click on frameworks, it's not going to show me all the series, it's going to show me the topics related to that taxonomy. And only when we click on one, and these are all dummy data, but only when you click on one would we show the series associated with that topic. Okay, so now we're seeing we have a series, we have taxonomies, and we have topics within.

one would we show the series associated with that topic. Okay, so now we're seeing we have a series, we have taxonomies, and we have topics within those taxonomies. You get the idea. So it sounds like a topic is related to a taxonomy. So we would have something like this, the taxonomy ID, and the name of the topic. Now if I switch back, you can see every topic includes, and again, this is dummy data, it's not related to Laravel, but every topic includes a description. So we'd have something like that. Now again, in real life, you might add things like a slug to the taxonomy and a topics table.

Linking Series via Pivot2:17

So we'd have something like that. Now again, in real life, you might add things like a slug to the taxonomy and a topics table. Every topic would probably have some kind of icon or a thumbnail associated with it, but this is the general shape. Okay, so now we just have one final piece of the puzzle. How do we associate a given series with a given topic? Well we use a pivot table for that. So we're probably going to have a table something like this, series_topic, and that would contain the series_id and the topic_id it's associated with. And with that, I think we have the full puzzle.

Generating Models and Migrations2:47

the series ID and the topic ID it's associated with. And with that, I think we have the full puzzle. So to recap, a taxonomy consists of topics, and a series can be assigned to that topic through this pivot table. All right, let's get started. So in a new terminal tab, we're beginning from scratch. So let's set up a model for a Series, and I want a migration and a factory for that. All right, let's do another one for a Taxonomy. So we're finding the nouns. And then of course, one more for a Topic.

So we're finding the nouns. And then of course, one more for a topic. All right, if we open up our database migrations table, let's start with the taxonomies. So as we discussed, a taxonomy just has a name. Table string for a name. Next, the topics table belongs to a taxonomy. So let's set that up. Table foreign ID for the taxonomy ID. Next, a topic has a name. And then is that it?

Next, a Topic has a name. And then is that it? It also has a description. So text for a description, but let's make that nullable. Okay, and that's your topics table. So what else? Let's do Series. So a Series consists of a title and a description for this demo. So you know what? Let's grab these two here, and that'll be title and description.

So you know what? Let's grab these two here, and that'll be title and description. And again, very simple for a demo, but it's enough to get the idea across. So I think we have our migrations in place. But there's one more. We haven't set up the link between a Series and a Topic. So we're going to do that right now. We're going to make a migration called create. And following conventions, it'll be series_topic table. So now if we take a look at that, yeah, we're going to set up a foreign ID for the series_id.

And following conventions, it'll be series topic table. So now if we take a look at that, yeah, we're going to set up a foreign ID for the series_id. So one more time, we're just following what we have here. We've already worked this out. And another one for the topic_id. However, both of these should be constrained. And then we'll say onDelete('cascade'). So we're just setting up the foreign keys as well as a foreign constraint for each one. Okay, so I think we're all set to go. So I'm going to go ahead and php artisan migrate the database.

Seeding Dummy Data5:00

Okay, so I think we're all set to go. So I'm going to go ahead and migrate the database. And there we go. If I switch to my database and refresh, sure enough, we have series and taxonomies and topics and our pivot table. Okay, so next, I want a little bit of dummy data. So we've set up some factories here. Let's do it really quick. And in fact, I won't even make you watch. For a series, we use a fake sentence for the title and description.

And in fact, I won't even make you watch. For a series, we use a fake sentence for the title and description. For a taxonomy, we'll just use a word. For a topic, we'll use a fake word for the name, a sentence for the description. And then for the taxonomy_id, that's a relationship. So a little tip, simply pass the factory call here. Don't run create, just pass it here. And that will create the taxonomy and set the ID if necessary. But if you override it, it won't call the create method at all. So now we have factories for topics, taxonomies, and series.

But if you override it, it won't call the create method at all. So now we have factories for topics, taxonomies, and series. And we can now whip up some data. So if I boot up php artisan tinker, I could say, give me a factory for a series. And let's do three of them. Next, let's create a handful of taxonomies. So I'm going to do the same thing here, create three taxonomies. And actually, you know what? Let's make it a little more readable. So we'll hard code this.

Let's make it a little more readable. So we'll hard code this. We'll say this one will be frameworks. And then we'll do another one for languages. And then another one for tools. OK. What else? What else? For a topic, what we'll do here is we'll do both. So let's say a topic, maybe the taxonomyId is frameworks.

For a topic, what we'll do here is we'll do both. So let's say a topic, maybe the taxonomy ID is frameworks. So a topic might be Laravel. And let's see what the ID is. Frameworks has an ID of 1. All right. Taxonomy ID is 1. And name is Laravel. We might have one for PHPUnit. It doesn't have to be php.

We might have one for phpunit. It doesn't have to be php. It could be Tailwind. It could be Symfony or Ruby on Rails. You get the idea. So now, if we switch back, we have various series. We have various taxonomies. And then we have topics that are associated with those taxonomies. Now, if we were to set up the Eloquent relationships, we might have something like this. A Taxonomy consists of Topics, right?

Defining Eloquent Relationships7:16

Now, if we were to set up the Eloquent relationships, we might have something like this. A Taxonomy consists of Topics, right? And that's a standard hasMany relationship. So this would be hasMany Topics. Because a Topic belongs to a Taxonomy. So in reverse, a Topic belongs to a Taxonomy. Next, though, a Series has many Topics, but through that pivot table, right? So if I wanted to say, give me all of the Topics associated with this Series, well, we're going to use a belongsToMany relationship. All right.

well, we're going to use a belongsToMany relationship. All right. So this will track down all of the topics by looking into the associated pivot table. All right. So are we looking good here? A taxonomy consists of topics. You can imagine helper methods like add a particular topic. So if you were to do that, well, you're just going to use the relationship. This topics save the given topic. So that will persist the new topic.

This topics save the given topic. So that will persist the new topic. For topic, yes, it belongs to a taxonomy. But of course, it's going to have the inverse here. So if you want to find all the series associated with a particular topic, it's also going to be a belongsToMany relationship. And now the same thing here. So if you have a given topic instance and you want to add a series to it, you might add an addSeries method. And in this case, we're not going to call the save method because we have

you might add an addSeries method. And in this case, we're not going to call the save method because we have a belongsToMany relationship. So we're going to use attach instead. So you might say something like $this->series->attach a new record where the series matches up here. Or sometimes you can even do things like this to avoid duplication. But this is fine. And that's it. That's your whole relationship structure there.

Attaching Series to Topics9:15

So let's figure out what topics are associated with that framework. All right. We have Rails and Symfony and Tailwind and php and Laravel. That's what we want. But now at the moment, we have no series associated with those topics. So why don't we find a topic one more time? Once again, just grab the first one. All right. Laravel. So we have this addSeries method.

Laravel. So we have this addSeries method. So let's say we have a Series. Let's just grab the first one we see. And now we'll say topic and series. Great. So now if you were to say, give me all of the Series associated with this topic, we get it. Or we still have our Series there. If I were to say, well, give me all of the Topics associated with this Series,

Or we still have our series there. If I were to say, well, give me all of the topics associated with this series, we can do the same in reverse. This series is associated with the Laravel topic, but it could also be associated with something else. And that's, again, why we use a belongsToMany relationship. So now we're not going to build the UI, but you can imagine what you need to do. You would need to start, for example, if I switch back, we have a section like this. You would need a landing page to show links for all of your taxonomies. And there you go.

And now you have them, and you would display them like so. Finally, when you click on one of these, you're going to fetch all of the series associated with that topic. Here's the topic you clicked, and we're going to find all of the series associated with it, in this case, one. So in your view, like right here, simply loop through the series and spit out your card. And that's it. So it really doesn't require much work at all to categorize your content according to any number of taxonomies.

So it really doesn't require much work at all to categorize your content according to any number of taxonomies. according to any number of taxonomies

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