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

Transcript

00:00
Okay in this episode we're going to look at pretty much every single way we can manipulate the tags that are attached to the two models that we have here, articles and courses. Basically how do we manage what tags are on an article, how do we add them, how do we go ahead and sync them, so how do we pick from a fresh list and sort of synchronize what's there, how do we delete them etc or detach
00:23
them is the correct term here. So we're going to keep the view here just so we can see what changes when we pick either article one or two. So let's go ahead and pluck out article one from the database and do the detaching attachment stuff up here and we'll see that change in the UI in real time. Okay so let's grab the article first of all so let's just head over to the database
00:44
and what do we have here under articles that's just an idea of one pretty straightforward so let's say article and let's say find one. Okay so we know that on our article model we have this tags relationship pretty straightforward we've already seen this so the first thing we're going to do is look at how we attach a tag so to do this it's pretty straightforward we've already
01:08
spoken about this it is attach so we just attach but the question is what do we pass into here well there's a couple of things that you can pass in just before we do this I'm going to head over to the database and I'm going to manually get rid of these two things so article one here has two tags I'm going to get rid of them and let's just go over to the browser and let's just have a look
01:33
here great so them tags have gone we can attach these just here so let's just say we wanted to attach a single tag well over in the database we're going to need to know what id this is this will probably come from your interface your admin panel let's attach Laravel to this so all we really need to do here is just pass in the id of the tag that we want to attach that's it that's all we
01:54
need to do to attach a single tag let's come back over to the browser give that a refresh and there we go that tag has now been attached now what happens and this is key what happens if we just keep refreshing the page and keep reattaching this Laravel tag let's give this a refresh and you can see that unfortunately what happens is it will add this multiple times by default attach will not
02:15
check if a tag already exists before it attaches it again it doesn't really care it's just going to keep adding these on there is a way around this which we're going to look at soon but let's go ahead and look at some of the other ways that we can specifically attach things first so if we come over to tagables let's get rid of all of these and let's go over and get that
02:32
refresh okay yes we're still in the position where we have two so i'm going to get rid of that and not refresh this page okay so the other way that you can attach is passing multiple ids so let's say we want to attach tag one and two i'm actually going to get rid of this one again let's give that a refresh and there we go so there were no tags attached but now Laravel and Vue have both
02:53
been attached and once again what you'll find is when you're using attach really really crucially if we just keep refreshing it's just going to multiply these over and over again which obviously is not what we want okay so there is another way which is to already have the existing tag model and this will probably be useful if you are if you already have the model enabled or if
03:14
you already have the model plucked out of the database if you're using something like root model binding etc so let's go ahead and say tag bind one and then let's just pass this tag directly into attach so as well as an id we can pass tag in id is useful if you're submitting a form and you have say a select drop down list where you want to attach the id specifically and the values
03:36
come through as the ids but if you ever have a tag like this or you have multiple which we'll look at in a second this works as well so let's just make sure we've got the tag pulled in and let's try this out so i'm going to go over and delete these tags here over to the database give that a refresh and there we go that model which is laravel has been passed in and that's been attached in the
03:55
database now as you'd imagine the same deal with ids we can have multiple tags and attach multiple tags as well so this would obviously be under some sort of where clause but as an example let's just say let's grab all tags and let's attach all tags to this article and again you would probably do something like you'd say find and pass in multiple ids or you would say where id
04:22
is a specific id or wherein in this case wherein id and then you'd have a list of ids here whatever you want to do and obviously would fetch them out here with get basically whatever you're doing this attach method and same with detach it's incredibly flexible so for now we're just going to say grab all of them and attach all of them so back over to the
04:41
database let's get rid of this let's give the page a refresh and there we go all of the tags that we have in the database have now been attached so we don't need to say stuff like attach many or you know whatever like that you just pass in either an array of ids a single id a single model or a collection of models really really simple to do so we're now going to look
05:02
at the opposite which does exactly the same thing just in reverse which is as you guessed detach so let's go and detach all of the tags that we currently have here so let's head over give that a refresh and there we go they're removed from that sort of pivot table and obviously we don't see them anymore we can go ahead and just reattach these tags really quickly there we go and let's
05:26
now go ahead and detach a few ids so let's say we just wanted to get rid of one and two there we go and we're just left with inertia which has an id of three so at this point you kind of get the idea attaching tags detaching tags makes sense you now know how flexible this method is there was one other thing or two other more methods we're going to cover here in this episode
05:51
and that is syncing and this is probably the most useful and probably where you're going to use most often so let's cover syncing and let's just cover this with ids just to keep this super simple so I'm going to go ahead and attach id one and two to this just making sure I don't already have any in the database we know that that attaches Laravel and Vue with an id of one and two them tags
06:13
now for a refresh we already saw earlier that as we keep adding these there's no unique check to what we are adding here so that's a real problem because obviously if you have an admin panel or even if you're letting users attach things this is going to eventually break you don't really want to have to set conditions around this so how do we solve this well we solve this with the sync method
06:39
which pretty much works in exactly the same way so let's go back to the start where we had no tags and let's just swap out the attach method here for sync and let's just see what happens so I'm going to give that a refresh and you can see sure enough this is added Laravel and Vue and it just looks exactly the same in the database there is no difference here by the way if you do want these
06:59
timestamps filled in just as a sort of pivot to this episode if you do want these filled in what you would do is you would say with timestamps much like any other pivot type relationship and then when they get added the timestamps will be filled okay so with sync the difference is now when I give this a refresh notice that the values are not getting re-added this is syncing the data
07:23
so it's working out are these two things already attached if they are just leave them as they are so what I can now do is I can pass in one two and three say three has been an addition to the total tags that we have if you imagine an admin panel where we have each of the tags listed in some sort of drop down so Laravel, Vue and Inertia let's say it starts at Laravel and Vue but we just want to
07:46
add a new tag what we can do is sync is add all of these IDs take all of these IDs send them down to our back end to sync all of them and they won't get re-added so that's where this is really useful so you can see by adding Laravel, Vue and Inertia that has now worked now there is a slight problem with sync in which this removes existing tags so let's go and say that we want to sync one and two
08:11
like this what do we think is going to happen we've already got one two and three attached but if we go ahead and sync one and two what that's going to do is it's going to get rid of inertia so effectively the sync method is kind of saying add the tags in the state that I am giving you so I want only one and two and three here that adds one two and three if I just now
08:35
want one that's fine that's going to go ahead and just put that back to one there are some circumstances where what you want to do is you want to sync but without detaching the existing ones so when is that going to be useful well let's imagine that we have one and two already in here so we've got Laravel and Vue as existing tags let's say that the way that we send the data
08:58
down from our front end to our back end is just an ID of three so we just want to add the tag of three which is inertia in this case but we don't want to remove the two existing ones we're just saying please can you just add this one back on sync it so we don't overwrite on add multiple but I just want to add this tag in so if we were to do that like this we've already seen that's just going to
09:20
get rid of them too and sync it with just the the inertia tag but what we're now going to do is swap this over to sync without detaching and we are just going to pass in three either in an array or on its own it doesn't really matter and now what's going to happen is it's going to look at this and it's going to say but I'm not going to detach the existing tags that aren't included in
09:43
the array that I'm passing through here I'm just going to add this on give that a refresh and there we go and we can just refresh this as multiple times and it's just never going to detach the old ones but it's never going to duplicate the one that we've specified so then two methods take a little bit of time to get used to which one we should use but obviously as you're writing your
10:01
code and as you're writing tests you can pretty much verify that the functionality works as expected okay so there are a couple of more things that we can do with saving and manipulating data in here we're going to leave that for a separate episode because they're less common and we've already covered quite a lot here

Episode summary

In this episode, we're diving deep into how to manage tags for our articles and courses models—basically covering all the ways you can attach, detach, and sync many-to-many relationships in Laravel. We start off by exploring the attach method: how to use it to add tags to an article (either by ID or by passing in the Tag model itself), and how to handle single or multiple tag attachments at once.

You'll see firsthand what happens if you keep attaching the same tag multiple times (spoiler: you'll get duplicates!) and learn how to avoid that problem. Then we flip things around and look at detach, showing you how to remove tags individually or clear them all out in one go.

Next, we dig into sync, which is super useful when you want to ensure the tags an article has exactly match a set of IDs—replacing any old ones. But what if you want to add tags without removing the existing ones? That's where syncWithoutDetaching comes in, letting you add new tags while keeping the current ones intact.

Throughout the episode, you'll get a feel for how flexible and powerful Eloquent's many-to-many relationship methods are, and you’ll see all the changes reflected in the UI in real time. By the end, you'll have a solid grip on attaching, detaching, and syncing tags—the core ways you'll handle many-to-many relationships in real-world Laravel apps. There's a couple more advanced methods we could use, but we'll save those for another episode!

Episode discussion

No comments, yet. Be the first!