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
06. One to Many: Saving

Transcript

00:00
So as you'd imagine, saving a polymorphic relationship with a one-to-many type is pretty much the same thing as when we have a one-to-one. Really, within Laravel, the only difference between a one-to-one relationship and a one-to-many relationship
00:15
is that when we access this and save it, we're expecting that we just have one record. So it's kind of like having a collection that just returns the first item. Okay, let's look at a couple of ways that we can save out a one-to-many polymorphic relationship. So we've got our article here. We already have a record over in the database, so we can go ahead and get rid of that for simplicity.
00:39
And what are we going to do here? Well, we're going to build up the comment that we want to save. So let's hop over to our comment model. We know that this is guarded. We know that it is part of this relationship. So let's go ahead and build this out. Now, when you would do this to a form, of course, you would go ahead and say something like comment, make, and you'd make this out with the data that you get through from your form.
01:05
So in our case, that's just going to be the body. It's probably going to be hooked up to a user ID as well. So we could say something like request only and body, or you could, you know, finding the user ID in there as well if you wanted to. In our case, we don't have that much data and we're not building out a real world example here.
01:25
So we're just going to manually fill in the body here. So let's say ABC. OK, so we've made that comment, not persisted it to the database. And of course, what we're going to do is from the article, we're going to access the comments relationship and we're going to do exactly the same thing.
01:37
We're going to use save to save that comment like that. Let's head over to the browser and give that a refresh. And now let's head over to the database, give that a refresh. There we go. So the sort of metadata associated with this, the important stuff has been stored, but it is now hooked up to that specific thing. And we already know that if this wasn't an article
01:57
and if it was an episode of a course and we save this in exactly the same way, it would just work in exactly the same way. So regardless of how your form is set up, as long as you're building up a comment with the basic information, you can just store it to the comments that you have here.

Episode summary

In this episode, we're taking a look at how to save records to a one-to-many polymorphic relationship in Laravel, specifically focusing on comments attached to articles (but the same approach could be used for any type of related model).

We start by reviewing how similar this process is to saving a one-to-one relationship—really, the main difference is that with one-to-many, we're dealing with collections of records, not just a single one. Then we walk through the entire workflow: building a new Comment (typically from form input, but in this example we're filling in the details manually), and then saving it via the article's comments relationship with the save method.

After saving, we check it out in the database, confirming that the record is properly hooked up to the article. And, as a bonus, we mention how versatile this method is—it works just the same if you're commenting on something else, like an episode instead of an article. So wherever your form data comes from, if you build the comment with the right info, saving it to the related model's comments will just work.

Episode discussion

No comments, yet. Be the first!