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
12. Many to Many: Saving with pivot data

Transcript

00:00
Using the methods that we've already covered like attach and sync and sync without detaching, we can also go ahead and pass in pivot
00:06
data and I'm going to show you a really practical example here with our articles and our courses that will allow us to demonstrate this. So the first thing that we need is a pivot column over in our taggables. So what's this going to be useful for?
00:22
Well let's think about this in the real world usage. We've got a bunch of articles and we've got a bunch of courses. Now we're attaching things like Laravel, Vue, Inertia, whatever to these. It would be super helpful if for every time we tagged something under Laravel, Vue or Inertia or any of the other tags
00:37
that we had, we could specify the specific version of Laravel, Vue or Inertia that we're actually covering for that article or course. So we're going to manually do this in the database just to save having to mess around with our migrations but I'm going to go ahead and add in a pivot
00:50
column in here and that's just going to be version. That's just going to be a varchar so let's just copy that over and really importantly we want to make this nullable because not everyone is going to have a version filled in. We don't want that, it's optional.
01:05
So now what we can do, let's just get rid of all of these taggables here to start from the beginning. When we go ahead and tag something we want to specify a version. So let's start with the basics here. Let's say article tags and attach. So what we were
01:22
looking at before rather than sync, these pivot things work for both and we want to go ahead and attach say tag1. As the second argument we can go ahead and pass in an array so we can have multiple of the things that we want to attach
01:39
into the pivot. So in this case let's say version and let's just keep this simple. I'm just going to say 1.0.0 obviously that's not the Laravel version that we would want to tag. Okay so this isn't going to work at the moment if we just go over
01:54
to the browser and give that a refresh. It does tag Laravel but if we just take a look in the database here, oh the version has been filled. Okay so I would have assumed we'd have to specify that. I think we should do that anyway. So if we go to article and say with pivot and let's say with
02:13
pivot version. Now this is useful if you want to access that pivot value not store it. So it still worked to store without with pivot but what I want to do is I want to show these over on the list of articles under each of the tags. How do we do that?
02:26
Well let's say tag and let's say pivot and just dump that out and see what we get. So this is the pivot table this is gives us direct access to that pivot table and you can see here that sure enough we've got the version. Now what I was
02:38
talking about earlier where this with pivot was in here, if we get rid of that you can see that the version here is not available. So that's why we added with pivot and version. So we now have access to that and obviously this is getting duplicated
02:53
because we are attaching every single time but we've now got access to that version. So what we can do over here is we can say pivot version. Now there's not always going to be a version we'll deal with that in just a second and I'm also going to go and get rid
03:05
of this and I'm going to bring this back to just one tag. We'll use sync in a minute and I'll show you how to do this but you can see now for every single tag unique tag that we add we can add in this pivot data which can be really useful.
03:19
So let's go over here and let's just well there's a couple of ways that we could do this you could create a giant if statement around this if you wanted to. If it didn't have a version so if this was actually null or empty we would just see nothing but
03:32
obviously you can handle that with any condition that you want. Okay so let's go and just re-tag that in the database and let's go over to our web routes and see what else we can do here. So let's use sync because to be honest the majority of the time you're going to be
03:47
using sync. So let's say article tags and sync. Now what would we want to do here? Well if we want to sync one and say just change the version let's go ahead and say version and we'll just say 2.0.0. Let's go over and give that a refresh now
04:03
and you can see that that has not changed. So with this what we would need to do it just doesn't work in the same way we would have to do this. Let's just start this again. We're going to say one that's the key of the thing that we're syncing. The array
04:18
value of that key within this array is then going to be what you need to change this to. So let's say 2.0.0. Let's give that a refresh and there we go that changes. So what you're going to have to do is you're going to have to build up an array
04:32
and this could be you know this could be from a form with additional data where you tag it. I'm not going to look at an example here because the way that the data comes in varies so much but this is how you want the data to end up. So let's say that we wanted to tag
04:49
tag 2 with 12. There we go. So obviously these are probably the opposite way around but you get the idea. So we can just keep changing these over and over again and again this works with sync without detaching as well. So if we then wanted to come in and keep
05:02
these two but we wanted to tag 3 as an additional tag with a new version we could very easily go ahead and do that as well if we use the correct method name. There we go. So the pivot stuff is really important because it adds context to each
05:21
individual thing that you attach with a many-to-many relationship. Hopefully this example makes that incredibly clear but you can transport this knowledge over to whatever you're attaching with a many-to-many relationship.

Episode summary

In this episode, we take a practical look at attaching pivot data to many-to-many relationships in Laravel. If you've ever found yourself needing to include extra information (like a version number) when connecting two models (think tagging articles or courses with framework versions), then this is exactly what you'll learn here.

We start by talking about why you might want extra data in your pivot table — like storing which version of Laravel or Vue an article is about. We then manually add a 'version' column to our pivot table and make it nullable, so not every tag needs to have a version specified.

From there, we walk through how to use the attach, sync, and syncWithoutDetaching methods to add data to the pivot table. You'll see how to pass an array of extra data (like ['version' => '1.0.0']) when attaching, and how Laravel then lets you access that data (using withPivot) when working with your relationships in code.

We also cover the right way to construct the data array when using sync, since it's a bit trickier than with attach. Finally, we wrap up by showing how all this works in practice, and how displaying or handling these extra pivot fields can be super useful in your apps.

By the end, you'll be ready to add context to your many-to-many relationships — whether for tagging, tracking, or any other advanced use case!

Episode discussion

No comments, yet. Be the first!