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

Polymorphic Relationships Overview0:04

Laravel gives us the ability to create a model that has relationships to multiple other models. And it does so by using a single table. And this is all thanks to the polymorphic relationship. Now that sounds a little daunting, but really it's quite simple. And we're gonna look at two polymorphic relationships in this episode. The first is going to be just what we could think of as a one-to-one kind of relationship.

Building Descriptions (Morph One)0:24

The first is going to be just what we could think of as a one-to-one kind of relationship. So we are going to make a model called description. The idea being that this is going to be a single table to where we store descriptions for other models like a user. So a user's description would be like a bio or a team's description. Could be the, well, the description of the team or it could be their mission statement or, or something. We could have descriptions for departments

or it could be their mission statement or, or something. We could have descriptions for departments or anything else that needed a description. But we're storing it all within this single table. And let's open up the create description, table migration so that we can actually create this table. We of course, need something for the actual description. So we're gonna have a column called content. The next thing that we need to define is with the morphs method.

The next thing that we need to define is with the morphs method. And this is where the magic kind of happens because we're gonna give this a name, we're gonna call it describable. The convention is to, well take whatever the name of our table is, descriptions, well, our description is describable. And this is going to not create a column. It's actually going to create two columns.

And this is going to not create a column. It's actually going to create two columns. The first column is going to be the describable id. This is essentially the foreign key. So if we create a description for a user, then the user ID will go into that describable id, but that's not enough. Eloquent also needs to know the model of this id. In which case there's going to be a describable type, which eloquent is going

In which case there's going to be a describable type, which eloquent is going to store the model class name. So here we will see app models user. If we were storing a description for a team, it would be app models team, and then of course the ID would be for the respective user or team. And you know, that's basically it. That's all that we need for this particular table.

And you know, that's basically it. That's all that we need for this particular table. But now we just need to open up the description model because first of all, we need to define the protected fillable array so that we can add a content whenever we need to. And then we need to define our relationship methods. Now in the case of our description, we are going to use that same term that we used whenever we called the morphs method.

that same term that we used whenever we called the morphs method. This is going to be the describable method Describable. That looks wrong, but I'm sure that that's right. We'll find out if it's not. And this is going to return a morph to relationship. And all we have to do here is return this morph to so that now we just need to go to our other models that we want to have a relationship with this description and set up that relationship so that

to have a relationship with this description and set up that relationship so that for our user, I'll do this. We'll have public function description and this is going to return a morph one relationship. So you can kind of think of this as a one-to-one relationship. So a user will have just one description. A team will have just one description,

So a user will have just one description. A team will have just one description, but we want to return this morph one. And then we of course need to specify the model that the relationship is with. But we also need to specify the name that we used for the morphs method, that describable prefix if you will, because those columns are prefixed with this term. And then we just need to do the same thing for every other model that we want a relationship with the description.

And then we just need to do the same thing for every other model that we want a relationship with the description. So we're gonna do the same thing for the team, and that's gonna be fine. So that now we just need to seed our database with this information. And we will do that by going to the database cder and you know, we create our teams, our departments, our users, all of that stuff. After everything, let's do this.

our users, all of that stuff. After everything, let's do this. We'll get the first user, we'll also get the first team, and then we will use the description relationship method so that we can create, you know, our description. The content for the user is gonna be senior developer. And then we just need to do the same thing for the team. But in this case, let's do something team related, like um, we build the backend systems

But in this case, let's do something team related, like um, we build the backend systems of the app, something like that. That oughta work fine. So with all of that in place, let's migrate fresh seed. Hopefully everything is going to run okay. And sure enough it does. And let's take a look at the database because, well, it's kind of important.

And let's take a look at the database because, well, it's kind of important. So here's our user's table. Nothing has changed as far as the user table is concerned. The same is true for the teams and that's because with the polymorphic relationship, everything is contained within, in this case the descriptions table. So we have the content, we have the describable type to where we see the user and the team models being listed here.

Customizing Type with Morph Map5:48

So we have the content, we have the describable type to where we see the user and the team models being listed here. And then we have the IDs of those respective model objects, user ID of one and team id of one. But we can also simplify how the model classes are stored within this type column. To do that, we need to open up the app service provider. Let's go to the boot method because here we are going to use relation morph map, and we're gonna pass in an array where the keys are the,

because here we are going to use relation morph map, and we're gonna pass in an array where the keys are the, the values that we want to use inside of the database. And then we just specify the model classes. So here, this is of course for the user model and we are going to do the same thing for teams. And later on we're gonna use the tasks. So we're gonna go ahead and set that up as well. And this just makes it a little bit easier to read inside of the database.

And this just makes it a little bit easier to read inside of the database. It's not something that we have to do, but it's something that I want to do. Now we do need use statements for these models. So let's add those and then let's reseed our database. Of course all of the random stuff is going to change, but our descriptions are going to be the same because we hardcoded those.

but our descriptions are going to be the same because we hardcoded those. So if we open back up descriptions, once again, we have the content, we have the describable IDs because those didn't change, but now look at the type users is now set for the user model teams is set for the teams model. So once again, it's not something that we have to do, but it does, you know, simplify things at least that is stored in the database.

Querying Description Relationship7:21

but it does, you know, simplify things at least that is stored in the database. But now of course we need to know how to get that description information. And you know by now, this is old hat, we know how to access the relationship information. So we're not gonna do anything fancy, we're just gonna get a user and output the description for that user. So that's really all we're gonna do is say user

and output the description for that user. So that's really all we're gonna do is say user with the description. Then we will get the first one so that we can get the description and we want to output the content. And if we were doing the same thing for a team, it would be the same thing because we know how to get to those relationship values. So inside of the browser, let's just go to user description

Creating Notes (Morph Many)8:02

because we know how to get to those relationship values. So inside of the browser, let's just go to user description and voila, we have senior developer. Now we're gonna take this a step further and talk about the morph mini relationship, which you can kind of think of as a one to mini relationship, except that it's polymorphic. And what we're going to do is create a model called a note. The idea being that, you know, a user can create multiple notes

The idea being that, you know, a user can create multiple notes or a team can have multiple notes. Uh, shoot, we can even create tasks or, or notes for a given task, you know, that kind of thing. And all we have to do is of course, set that up. So first of all, let's open up the create notes migration so that, uh, what do we have? Let's just call it content because it's the content of the note.

Let's just call it content because it's the content of the note. And then we need to call the morphs method. And then we're gonna give this a name of simply notable. And once again, this is going to create two fields. The first will be notable ID for the foreign key, and then the notable type, which will be the model class. And that's it. But of course we need to set up the relationship in all of our models.

But of course we need to set up the relationship in all of our models. So let's start with the note model. And we want the protected fillable array and content is the only thing that we need to be concerned with. And just like the descriptions, we are going to have the notable method here, and it is simply going to return a morph to relationship. So we'll call this morph two and that will be that.

and it is simply going to return a morph to relationship. So we'll call this morph two and that will be that. But then we just need to set up the relationship and all of the other models. So let's do user, let's do team, and let's do task. And we could do departments if we wanted to, but let's keep things at least a little simple and just like description, we're gonna have our notes, but in this case, it's a one to many relationship.

and just like description, we're gonna have our notes, but in this case, it's a one to many relationship. So we have multiple notes, not just a single note. And this is going to return a morph, many relationship so that we will return this morph many. And just like with the description, we specify the model that it will have a relationship with, and then the name of that morph field, which is notable. Then we just need to take this method and copy and paste it into all of the other models that we want

Then we just need to take this method and copy and paste it into all of the other models that we want this morph many relationship in. And I guess it added that automatically. Did it add morph? Many Sure enough. Okay, I'm not gonna worry about that. So there we go. Now we just need to seed our database with that information. So let's open up the database cer and what do I want to do? We already have our users and our teams. So let's do this. Let's use our user first so

We already have our users and our teams. So let's do this. Let's use our user first so that we can create a note that says poly relationships, our cool, that could be taken in a couple of different ways. Um, let's say polymorphic relationships. And let's just create another note for that user, in which case, um, we'll, we'll say morph. Many is cool because everything is cool and we won't worry about everything else

Many is cool because everything is cool and we won't worry about everything else because it's gonna work the same way, whether if it's a user or a team or a task or whatever. Uh, so we have two notes for our user. Let's seed our database and then we will fetch that information so that we can see it inside of the browser. So let's create a new endpoint. We have user description. Let's create a user notes to

So let's create a new endpoint. We have user description. Let's create a user notes to where we will get a user with the notes relationship. We'll get the first user, because that's the only user that has notes so that we will get the notes. We aren't going to work with individual content, we're just going to return that so that inside of the browser we can go to user notes. And voila, we have the notes for that given user.

Recap and Next Steps12:27

of the browser we can go to user notes. And voila, we have the notes for that given user. So polymorphic relationships are well, while they sound daunting, they're ultimately very simple and very powerful. They allow you to define a relationship between one model and multiple other models. You can use the morph one relationship to create what is essentially a one-to-one, or you can create a morph mini, which replicates the one

what is essentially a one-to-one, or you can create a morph mini, which replicates the one to many relationship. In the next episode, we are going to look at the many to many polymorphic relationship.

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