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
13. Many to Many: Relationship inverse

Transcript

00:00
Okay, so we're now going to talk about the inverse of the relationship with a many-to-many polymorphic relationship type. What do I mean by that? Well, I mean for any of the tags that we have assigned, I want to find out which articles sit under those tags.
00:17
So to demonstrate this, we're first of all going to head over to our code and let's go ahead and sync a particular tag here to Article 1, or Article 2, and we'll sync Tag 1. So basically, when we click on View in Inertia, we're going to build out a full example here, we should see Article 1. When we click on Laravel as a tag and we have a URL that lists out all the articles that exist within that tag, we should see Article 1 and Article 2. Now, even though we are still continuing to use this as an example for articles, the relationship that we're going to set up will also work for the courses as well.
00:51
Remember that whatever we're doing here applies to both of the things because, of course, it's a polymorphic relationship. Okay, let's go ahead and get rid of the example that we have here, and I'm just going to build out a really simple route here. We're not going to bother with controllers or anything like that. We're just going to have a get route, which allows us to view a particular tag, and we'll use Root Model Binding here and just a closure to go ahead and grab that tag out.
01:15
And for now, we'll just go ahead and die dump that tag, but we'll build out a really simple view here to get this working. So over on the homepage now for each of these tags, I'm going to go ahead and link them through to that specific page. So let's go ahead and wrap that in an anchor. And actually, let's go ahead and give this a name.
01:31
That makes sense. Let's say name, tags.show, tags.show, and we'll pass the tag in there to fill in the Root Model Binding. So if I click on Laravel, yep, sure enough, there we go. We get the Laravel tag, same for any of these as well.
01:47
So what we are now going to do is create out a view here, let's call this tags.show. And over in our web routes, we'll go ahead and return a view, tags.show. And then down here, we're going to pass the tag. Let's just pass the tag first.
02:08
Well, there are other ways that we can do this, particularly if we're wanting to show multiple items that are under a different tag. So we want to show articles and courses, there's a better way. So let's go over and output this. So let's go over to tag.show.
02:26
And let's go ahead and start to iterate through what we kind of think is going to happen. So let's say for each tag, what do we think we're going to need here? Well, it's going to be some articles, isn't it? Because we are looking at an inverse relationship on a tag.
02:43
We want to find out which articles are part of this tag. So then we're going to say as article. And then we're just going to dump out the article title in here. So let's just keep this super simple.
02:56
And we would probably at the top of here, show the tag. What do we call that? Title as well. So let's go ahead and add in the tag title.
03:09
OK, now at the moment when we click on one of these, obviously it's going to break because we don't have a relationship here for articles. That's where the inverse relationship comes in. So in here, what we want to do is create out obviously articles and we'll have to have two of these, one for courses and one for articles. And the relationship type here is morphed by many.
03:31
Then we just relate it back to the thing that we're looking at, which is an article. Like so. Again, we give the sort of name of the polymorphic relationship. In this case, it is tagable and you can continue to add any other items you want on here, including fetching pivot information as well if you need to. So now that we've got that relationship in there, let's give it a refresh. And there we go.
03:52
So for the Laravel tag, we've got two articles here. For the view tag, we've only got one article, which is article one. And for the inertia tag, we've only got one article just here. So you kind of get the idea.
04:04
Now, if you want to take it further, of course, what you would do is just create a completely separate relationship here for the courses. I don't think we have anything tagged up at the moment for each of these, but we can go ahead and do that in the database. So we could say h2 articles. Just keeping this really, really rough for now.
04:24
And we could say courses, iterate over each course, and then say course and course title. And let's just give that a refresh. And yeah, we don't have any courses hooked up just yet, but let's just do that in the database really, really quickly. And obviously, we already know how to store this stuff. So let's say course one. And let's go back over. There we go. Great.
04:49
So this gives you a really good idea of how you can create a completely flexible system where you can dive into tags if you want to see what content is associated with that tag. And of course, over here, we have the tags per article. Now, just very quickly, you can use pivot information here, like I said. So for each of these articles that we have here, we can access the pivot information. So we can say article pivot in here and version.
05:19
Now, let's just take a look at this really quickly and click on this tag. Now, you can't see the pivot information at the moment. That's just because, as I mentioned, when we set up the reverse relationship, we're going to need to do the same thing that we did on the tag relationship. And that is with pivot and then passing any of the pivots that we are using here.
05:36
Same thing if you're creating courses through tags, which is probably more rare, or creating articles through tags. You might want to see it with timestamps as well. You might want to order this, whatever you want to do. I'll leave that off just for now, though. So we've got the with pivot version for both of these because that might be useful to show. And when we give that a refresh, sure enough, we see the item in there.
06:00
We don't see it for this one. And let's just check why that is. And yeah, I just didn't output it. So the same is going to work here for the course pivot version, because, of course, both of them use a common pivot table with the version specified in here. So now you have a super flexible system where you can dive into tags.
06:20
But of course, you can also see all of your articles, courses with the tags enabled. That creates a really nice system where we can click into one, we can click into an article, we can show the tags for that and we can click on the tags for that and go back to the tag page. You can do pretty much anything you need now with these two relationships.

Episode summary

In this episode, we're diving into how to handle the inverse side of a polymorphic many-to-many relationship. Instead of just seeing which tags are attached to an article (like we've done already), we're flipping things around: for a given tag, we'll see which articles (or even courses) are linked to it.

We start by syncing some tags with articles as a demo. Then, we build out a simple route to view a tag and show all articles associated with it. There's no controller right now – just a quick closure and some basic routing with route model binding. We update the homepage so that tags are clickable, leading to a tag details page with all its related content.

As we set up the new view for tags, we talk through how the inverse relationship works in Laravel and implement it in code using morphedByMany. This lets us pull out all articles and courses for a tag, and we show how to expand this for different content types.

We also add in support for showing pivot table data (like a version number) in the tag details page. The same trick lets you show extra info, like timestamps, or order results if you want. Finishing up, now you have a fully flexible system: you can click into a tag, see all connected resources, view their extra pivot information, and of course, jump right back to the article side as well.

Episode discussion

No comments, yet. Be the first!