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
22. Detaching relations

Episodes

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

Transcript

00:00
Now that we have looked at attaching, we're going to quickly look at detaching. We're not going to go for a practical example here, but you can use what we learned in the last episode to pull this together into your own apps.
00:09
Okay. So let's go ahead and create out a route in here, which will allow us to detach something, and we'll take a look at how we can do this. You probably already guessed how.
00:19
So we know that we want to grab what we want to detach from. So we're going to go ahead and grab the course that we want to detach from. Once again, that's just going to be the same one that we had before. So let's just grab both of these and put them in here.
00:36
So we've got learn inertia and Laravel. I think we don't have anything attached to that just yet, but we can go ahead and use our practical example to do that. Okay.
00:45
So let's make sure we have some actual topics on here first. So let's go and head over to course two. We'll attach Laravel. We'll go ahead and attach eloquent and we'll go ahead and attach inertia.
00:58
It doesn't really make sense, but we have three in here now and we can start to detach these. Okay. So we're going to find a topic Laravel and for a course, we want
01:07
to go ahead and get rid of this. So we're going to say course again, topics, and you probably guessed it, we're going to use detach. So we can pass through either the ID or the actual topic itself to this.
01:19
And that's going to go ahead and remove that for us. So similar UI, if you had a list of them, you could post through to delete these basically. Okay.
01:27
Let's head over and just give the detach page a refresh and see what happens. If we head over, we should see Laravel disappear from this list. And sure enough, we do. So we could do that for all of these if we wanted to, and that would go
01:41
ahead and get rid of each of them. You kind of get the idea. Detach just detaches exactly the same as attach very simple method, but obviously just the opposite.

Episode summary

In this episode, we shift gears from attaching relationships to learning how to detach them. After a quick recap, we set up a new route that will let us detach topics from a course (and you probably already have a guess how it's done if you saw the previous episode!).

First, we make sure our course actually has topics attached—like Laravel, Eloquent, and Inertia—just so we have something to work with. Then, we show how to use the detach method to remove a topic (like Laravel) from the course. It's as simple as specifying the topic you want to get rid of and calling detach on the relationship.

We walk through refreshing the page and seeing the topic disappear, confirming it's really gone. This episode is a straightforward demo showing that detaching relationships is just as easy as attaching them—just the opposite!

By the end, you'll see how you could let users remove topics from a course in your own apps using a similar approach.

Episode discussion

No comments, yet. Be the first!