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
28. Syncing relations

Episodes

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

Transcript

00:00
We've already covered attaching and detaching relationships, but there's a very, very important way of syncing relationships that you'll need to know about when you're working with many-to-many relationship types.
00:13
So let's demonstrate the problem that this gives us, and then we'll go ahead and implement the solution. Okay, so with eloquent relationships, by example, let's just double check the ID of this course, that is the first one.
00:27
Let's head over and try and attach Laravel again, see what happens. So we're gonna head over to courses slash one slash topics. Let's try and attach Laravel again, and again, we can give a version, it doesn't really matter here.
00:40
So if we go back to our homepage, I think I might have just attached that again, we have two topics in here, both with Laravel, both with version nine,
00:51
although the version can change with that intermediate pivot value. So this is a bit of a problem. What do we do about this? How can we get around this when we don't want two instances of these things in here?
01:04
Well, of course, what we could do is somewhere, create some sort of if statement to check this, maybe detach it if it already exists. You know, there's a load of complex ways that we can do this,
01:16
but Laravel provides us a solution, which is syncing our associations. So I'm gonna go ahead and show you this just within a web route really quickly. So we can get to grips with how this works, and then we'll go ahead and apply this to the functionality that we've already got.
01:31
So we need to go ahead and pluck out a course. So let's go ahead and do that really quickly. We'll grab the first course just here, and then let's just not grab any topics out just yet.
01:42
And we wanna go ahead and sync these up. So I'm gonna head over to our course topic table, get rid of this just so we can see what happens when we run this code. Let's make sure that we have this open just in here, like so.
01:54
Okay, so how do we do this? We're gonna say course topic in exactly the same way, but this time we're gonna use the sync method. Now the sync method allows you to pass in a list of IDs, kind of like attach.
02:05
So let's attach three topics to this particular course and just see what happens. So give that a refresh, come over, and the topic IDs one, two, and three have been attached. This looks like on the surface,
02:20
it's working exactly the same as the attach method. But what we can now do with this is we can say, well, I wanna go ahead and sync these. I want to include topics two and three.
02:31
Now what's gonna happen behind the scenes is Laravel is gonna automatically remove topic one because we just want two and three in there. It's not gonna re-add two and three,
02:41
it's gonna synchronize this so we just have two and three. Let's go over and give the page a refresh here, come over to the database, and you can see sure enough two and three have been added.
02:51
So this saves you having to manually check if a topic or any kind of association already exists and having to manually remove it and adding loads of complex code to get this to work.
03:02
So what we could do here is we could just sync this with an empty array as well, and that's gonna go ahead and just get rid of everything for us. It works regardless of what you pass in. So let's say that we already have topic one and we sync this up
03:16
and we want to add a new topic. What do we do? Well, if we were to just pass down topic ID two like this and then add this to our sync method,
03:27
we already know what's gonna happen. It's gonna get rid of topic one and it's gonna kind of replace that with topic two. So we also have a method called sync without detaching like so.
03:39
What that's gonna do is sync these up, but it's not gonna detach anything that doesn't already exist or hasn't been included in this array. So sync without detaching, I can add two and three
03:49
and that's just gonna add them to here. So let's just take a look and come over and there we go. Now you might be thinking, well, isn't sync without detaching exactly the same thing as attach
04:00
if it's just attach them and not remove the existing ones? Well, it kind of works in the same way, but the difference now is if I were to add topics one, two, and three again, we already know that with attach, that's gonna attach another one.
04:13
So we're gonna have a duplicate. But if I were to refresh this as many times as possible and we come over, you can see that we only still have one, two, and three because behind the scenes, we already know that these exist.
04:26
So they've not been reattached. So sync is probably what you're gonna use in the majority of cases to just make sure that you're not attaching additional ones. So again, I could get rid of one and I could say,
04:39
well, I just want one, two, three, and four. That's gonna work again in exactly the same way. Not gonna read two and three, but it is gonna add four. And in our case, one is being kept as well.
04:50
But with sync, that would have removed one. So really just experiment with how you need this to actually work in reality, depending on how your UI works, what kind of data you're passing down. And this will sync everything up for you nicely.
05:03
You might also be wondering, well, if we're syncing like this, how do we attach our pivot data like our version that we've already included? Well, let's get rid of everything that we've got in the database. And let's just go with a really simple example.
05:15
And we're gonna sync the ID of one for that topic. All we do is we change this into a key value pair. So what we can do now is add in another array for this specific value. And we could bring this down so it makes a little bit more sense.
05:29
And in here, we can say version 10. So let's go over, give that a refresh, come over. And that has been inserted. So really, just depending on how your UI works,
05:40
you want to build up the key, which is the topic ID, and then the version which will come from your request. And you can do this for any of the items that you're passing down. So you can just do this, for example.
05:52
And if we come over, give that a refresh, you can see that works as normal. So in our case, what we can actually do with this example, we can go to where we are attaching our topics to our courses just here. And instead of using attach, we could actually use sync here.
06:10
We could go ahead and pass an ID here. So we're going to go ahead and assume that we are validating this. And get rid of this or add in this additional array. So now we're syncing the topic that we want with the version that has been passed in.
06:27
So the difference with this is this is going to go ahead and detach anything we don't pass through. So in this specific example, we would want sync without detaching in here. So let's go ahead and see the difference.
06:39
If we come over to courses slash one slash topics, we have a couple in there already. So we can just try and reattach Laravel, say version five. It does go ahead and replace that pivot data, but it does not add an additional Laravel topic to that particular course.
06:56
So you now pretty much have all of the methods available to you to build this up in the way that you need when you're working with many to many relationships. There are a couple more specific examples in the Laravel documentation.
07:08
So if you have something that's not quite working out or you need slightly more powerful functionality, now that you've got a good grasp on the basics, you can head over to the docs to find out more.
33 episodes4 hrs 18 mins

Overview

Eloquent is Laravel's ORM (Object Relational Mapper). In simple terms, it's how your models work with the database.

The good news? There's a bunch of powerful relationship types available. Our task is to learn when and where to use each one.

In this course, we'll cover each basic relationship type, how to access related models, and then insert, sync, update and delete related data. Oh, and we'll build a practical example for each relationship type, to really make it stick.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.