This episode is for members only

Sign up to access "Mastering Polymorphic Relationships" right now.

Get started
Already a member? Sign in to continue
Playing
05. One to Many: Defining the relationship

Transcript

00:00
So probably the most common polymorphic type you'll go ahead and use is a one-to-many, pretty much the same as a normal one-to-many within Eloquent. You use that probably the
00:11
most often. Users have many posts, things have many comments, things have many likes, all that kind of stuff. So we're going to roll with the example of a comment system. Now this is my favourite example because it is incredibly easy to understand. Let's think about a website. So let's say that we have an article section on the website
00:34
that has comments, so we can make comments on that. Let's also imagine that you're watching a course episode and each episode can have comments, which is exactly what you're doing right now. So this is a polymorphic relationship, how this is set up. Comments can be attached to multiple models or something can be attached to multiple models. We'll go with the comments
00:54
example because it's just the easiest to visualize. Okay, so we are going to go ahead and just create our new model. We're working within the same project here but this is fine. So let's go ahead and make out a model here called article with a migration and a factory. And let's also imagine that we have an episode as well that can have comments on it. We also need
01:17
the comment model itself. So let's go ahead and create out a comment model for that and we're done. So let's fill all of these in. So let's go over to the articles table. Again we'll just keep this really really simple. We don't really care what data is within here. So let's say string and title. We'll do the same thing for the episode. So let's open up the episode table
01:39
and we'll also have a title for that as well. So let's now fill in the create comments table migration and this is going to have let's just say a piece of text there with a body. So as you've guessed what I'm now going to do is I'm going to go ahead and create out a morph in here. So I'm going to say morphs and we're going to say commentable. So again we're taking the singular
02:04
name the model that we're working with and we're adding able onto the end of this. So let's go ahead and migrate everything. We'll fill in our factories super quickly. In fact we don't really need to do this. We'll let's ignore the factories for now. So what we're going to do is create out two things. We're going to create out a article and we're going to create out a episode which
02:26
both can have many comments. So pretty much like the one-to-one polymorphic relationship but of course it's now one-to-many. So just before we do this we'll focus on the comment model. So let's go in here. I'm going to go ahead and set this as guarded false so we don't get any math assignment issues in here and now we need to define out that this is commentable like we did
02:52
with our image earlier. So again we create out a method in here commentable. You can type in the return type again if you want to and we're going to say this morph2 like we did before. So morph2 and there we go. So now what we can do is create a morph many on any of the things that are allowing us to have comments. So that is our article model and again I'm just going to go ahead and set this
03:17
to guard. I should really do this globally but let's just do this inline on each model for now and here we're going to have comments. So just like a normal relationship we're going to say this morph many and of course we're going to reference the comment model itself and we're going to give the name of the commentable thing in the database. So we'll do exactly the same thing
03:41
here for our what are we on at the moment article. We'll do the same thing for our episode and we'll set that guarded as well. So let's grab all of this so open up our episode model and let's go ahead and add this into here. Great so we're doing exactly the same thing here. Let's go over and check out what this looks like. So we're going to do a little bit of manual creation in the database
04:05
first of all. So let's go over to our episodes and let's create out a introduction episode here. The created and updated at date. Now we know the model location to this but we're also going to change over our morph map to include episodes and we'll do the same thing for articles as well. So let's create out an article and let's fill in the created at date properly and the updated at date
04:37
as well. Okay great so now that we've got these two things I'm going to go over and immediately go over to my app service provider and I'm going to register these in here so when we eventually get to create them or read from them it's going to work nicely. So we've got article and that's going to hook up to obviously our article and we've got our episode now which is going to hook up
05:00
to our episode model. Great so now that this is done we should be able to just go ahead and read these. So let's just grab the article first of all and we already know it's going to work in the same way. So let's say article find one and let's go ahead and die dump on article comments. Now the only difference between this of course and the one-to-one is that we now have a collection even
05:23
if it's empty we're going to have an empty collection of items back so you can see here we've got no items within this collection that's pretty obvious because we haven't added any comments. So now we can set up a comment system we'll look at creating these relationships in the next episode but now we can go ahead and create a comment out but we can hook it up when we insert
05:41
these to whatever it is. So in this case it's going to be an article with an id of one created out like this. So now that article has one comment and we can assign it to the episode as well if we want to. So now that we've understood this again pretty similar to one-to-one relationships except we're just doing one-to-many let's look at inserting these in the next episode.

Episode summary

In this episode, we dive into the super common one-to-many polymorphic relationship that you’ll often use in Laravel’s Eloquent ORM. Basically, we’re setting up a classic comment system: both articles and episodes can have many comments (sound familiar?). We start by quickly creating models and migrations for articles, episodes, and comments, focusing on simplicity and leaving out unnecessary stuff for now.

We go through setting up the migrations, paying special attention to the morphs method to make the comments polymorphic. Then we move to the models, where we add the morphMany and morphTo relationships so everything links up nicely (just like a regular one-to-many, but with the polymorphic twist). We also make sure to hook up Laravel’s morph map in the service provider so Eloquent knows how to retrieve these related models.

To wrap up, we try things out by creating some episodes and articles and confirm that you get a collection of comments with your articles or episodes. We peek at what happens when you query the relationships and touch on how you’d insert comments related to different models. Next up, we’ll look at actually inserting these relationships in real-world code!

Episode discussion

No comments, yet. Be the first!