This episode is for members only

Sign up to access "Eloquent Relationships By Example" right now.

Get started
Already a member? Sign in to continue
Playing
23. Attaching and detaching multiple relations

Episodes

0%
Your progress
  • Total: 4h 18m
  • Played: 0m
  • Remaining: 4h 18m
Join or sign in to track your progress

Transcript

00:00
Depending on how your app is going to work, you might find it more convenient to attach or detach multiple things at the same time. So that's what we're going to cover here in this episode.
00:09
So we're going to head over to our attach route and I'm going to duplicate this down just so we have a reference to it and we're going to call this attach many just so we can see how this works. So we'll get rid of our topic just in here and we'll get rid of the topic that's being passed in and let's figure out how we could do this. So the topic or the course that we've got
00:28
here is learn inertia. Again I'm going to get rid of all of these in here and we're going to head over and look at how we can attach multiple things. So we'll head over to attach many and just see this we'll probably see an error at the moment or nothing happen and let's take a look at how we do this. So the first thing that we can do is pass through an array to this instead of the
00:50
model or the id we can just pass an array through. So I could attach every single topic here if I wanted to. So let's head over to our topics table and you can see we've got ids 1 through 4. So I can say 1, 2, 3 and 4 as simple as that and that will attach every single topic to that course. Let's give that a refresh. Let's head over to our main page here and you can see sure enough for a
01:12
learn inertia we have attached every single of them. Now we can do the same thing for detaching as well. You guessed it. So we can say detach many. Now in this case what we might want to do is find the learn inertia course and just detach say two of these. So let's say we wanted to detach ids 3 and 4 from this. That is going to work as well. So let's keep this page open. Let's head
01:38
over to detach many, head back over and sure enough they have gone. So as simple as that to detach and attach multiple in one go. So if your UI involves building up a list of multiple topics and you had access to an array of all of them ids you could just pass these directly into here. Let's just really quickly before we go looking at attaching multiple of these directly from our model. So I'm
02:05
going to come over to course topic and just get rid of all of these and we'll start from scratch here. But this time we're going to go ahead and grab out all of our topics. So I'm going to say topic and get. Let's see what happens just if we pass through a collection of these topics. Remember here we get back a Laravel collection to this attach method and let's just see what happens here.
02:25
So I'm going to go ahead and pass through all of our topics and attach them to that particular course. So let's come back over to attach many and see what happens. Head over give that a refresh and sure enough Laravel behind the scenes has worked out that we have a collection of topics. It has gone in grabbed out the ids for each of them constructed out an array for us and it has gone
02:46
ahead and attached them. So we can do that as well if you want to. So we can either do this by passing in a manual array or we can go ahead and pass in just a collection of topics and the same will work for detaching as well. Let's just go ahead and demonstrate that before we go. I'm just going to comment these two out and we'll go over to detach many. So let's go and create our topics in here.
03:12
Topic and get and let's go ahead and duplicate this line down and we're going to go ahead and detach every single topic from here. Let's go over we know that we've got four at the moment so because we are passing in all of our topics when we say detach many that's going to go ahead and get rid of all of them that we have passed in here. Now this isn't always very useful. I don't
03:34
find myself using this too often. There is a much better way to do that and that is syncing relationships which we are going to take a look at in this section as well. So we'll cover that later but this will hopefully give you a good idea as to how this works. We can attach and detach multiple records relate and relations by using either a collection of them things or a list of ids.

Episode summary

In this episode, we're focusing on how to attach and detach multiple related models in Laravel at once. Instead of adding or removing one relation at a time, you'll see how simple it is to work with arrays or collections to handle batch operations.

We start by duplicating our attach route, calling it "attach many," and see how you can pass an array of IDs to link all your topics to a course in one go. For example, if you have topic IDs 1 through 4, you simply pass [1, 2, 3, 4] and attach them all at once. It's just as easy on the detach side—you can detach multiple topics by giving their IDs in an array.

But what if you want to work directly with Eloquent collections, not arrays? We check out how Laravel handles it effortlessly: you can pass a collection of topic models directly into the attach method, and Laravel figures it out behind the scenes, attaching all those relations. The same trick works with detaching—just pass a collection to detach.

While this is super handy when your UI or logic gathers multiple IDs or models, be aware that if you want to update the full list of related things, just detaching all and reattaching isn't optimal. Toward the end, we hint that there's an even better way—syncing relationships—which we'll explore later in this section. For now, you should have a solid grasp on efficiently attaching and detaching many related records at once using arrays or collections in Laravel!

Episode discussion

No comments, yet. Be the first!