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
21. Attaching relations

Episodes

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

Transcript

00:00
OK, so let's take a look at how we attach these relations directly in code via the relationship. And then we'll go ahead and pull together a really basic example of how we can do this in a UI. So we're going to start out with a basic route in here just to demonstrate this.
00:16
And I'm going to go ahead and call that attach. And let's go ahead and head over to the browser here. Like so. Great. So just before we start, let's head over to our course topic and we'll get rid of this one here. So we want to attach within code to the Learn Inertia course, the Laravel topic. So let's save that out. So we go back to normal and let's look at how we do this.
00:41
The first thing that we're going to need is to pluck out or pull out somehow an instance of the topic that we want to attach to the course. We basically want the course and the topic and then we're going to hook them together. So we'll do the course first. So we're going to say course and find. And that could come through route model binding, could come through any way at all.
01:01
And let's find the ID for this. So it's idea two. So we've got the Learn Inertia course just here and then we want the topic. So we're going to say topic and find. And let's find the ID of this. That is. One, so we want to attach that, so one, that is Laravel.
01:23
Now we need to figure out how to hook these two things together. We know that the relationship is defined over on the course itself. You can do this the other way round. So let's start with the course and we're going to say topics. Now, with this, we don't want to say create because we're not creating anything.
01:40
This topic already exists in a separate table and we already have an instance of it. We don't want to associate it, which is what we've seen before where it fills in an ID for us because we don't have an ID within the course model itself that needs to be hooked up. Instead, what we use is attach. And you guessed it.
02:00
What we pass into attach is the topic that we want to attach to this course. So let's go ahead and do this. Let's see what happens. So we're going to head over to here and give this a refresh. We're going to come over to our course topic table, give it a refresh.
02:15
And there we go. As you can see, this has now been attached to what we needed. So we've done that now very simply in code using this. So in reality, what you are going to find is if you're building out the functionality to attach something to something else, you're going to need to know what you're attaching to and you're going to need to know the topic or whatever model you're attaching as well.
02:39
So let's look at a really basic example of how we would do that in the most practical way possible. So we'll keep this example in here, but we'll go ahead and create a new route for this. And we're going to keep this really, really basic. We won't create any controllers. And I'm going to create an endpoint called courses and then the course.
02:55
So we know that that is what we want to attach the topic to. And then we're going to say slash topics. Now into the request for this, we're going to get the topic ID that we want to attach. We can look that up and then we can attach it. So I'm going to get rid of everything that we've got in here. And then what I'm going to do is set this as a get route.
03:17
And then maybe let's create out another route in here, which will be a post route. So this will actually store it. And this will show a list of topics that we want to attach. Of course, the URLs could vary. Just depends on how you want to structure this. So the route that we're going to have here is going to show out a topics page and that's going to list out all of our topics.
03:39
So let's just say topic and get and we're going to build up a select here that we can use to submit this through to the course that we want. We'll also pass the course down because we're going to need to know how we want to submit that through and we should be good. So let's create out this view in here. Topics.blay.php. And what we want to do in here is build up a form with a select that goes ahead and lists through each of these options.
04:06
So option here. And we're going to do a foreach on these options. Topics as topic. And then we'll end the foreach there. And in here, we're going to output the topic name. We'll fill everything else in in just a moment. So topic title. And let's just head over to this.
04:26
So courses slash one slash topics. And there we go. Here are a list of our topics. We're going to have a button here which will submit this to attach it through. So let's create a button with a type of submit and say attach. And there we go. So for course one, which is we should have really added slugs to courses.
04:45
Let's find eloquent relationship by examples. Let's go ahead and look at attaching. I'm going to get rid of all of these just so we can sort of start fresh. And I'm also going to go ahead and duplicate this over and head over to our homepage so we can see these. And then once we have attached them, we'll come back and see if this has worked.
05:01
OK, so we want to attach a topic to that specific course. The first thing that we're going to need is a value. So we're going to need to know the topic ID or the topic slug, depending on what you want to use inside of this input. And we need to give this a name as well. So we're going to call this topic underscore ID.
05:19
We can get rid of the ID here as well. So we want this to submit through to here, which is going to have the topic ID in the request, and it's going to know which course we're working with. So why don't we just give this a name to make it a little bit easier? So we're going to say courses.topics.store. And we'll go ahead and grab the name of this and we will add this in here.
05:39
So we're going to create our root helper, reference that. We're going to set the method here to post and we'll go ahead and include our cross site request forgery token. So now when we submit this through inside of here, we will be able to die dump on the course, but we will also get from the request the topic ID that we want to attach to that course.
06:00
So let's make sure we bring the course just into here and let's see what this looks like. And if you're confused, don't worry, I'll go over this in just a second. So, yeah, let's have a look here. Missing parameter. Yeah, of course, we just need to make sure we specify in here which course this is actually for and we should be good.
06:17
And yeah, we're not passing that down. So let's make sure we figure that out as well. Course and course. Great. OK, so third time lucky. There we go. OK, so I want to attach Laravel to this course. The first thing we get dumped out is the course. The second thing is the ID of the topic that we want to attach.
06:35
Great. So now that we have our form and we have that data all rolling through, all that's left to do is attach it in here, redirect back and we should see that attached. So let's go ahead and do that. We're going to say course and topics and attach. And now we need to pass in the topic itself here, not the ID we want to pass in the topic.
06:58
Let's just try passing the ID in and just see what happens. So I'm going to say request topic ID. Let's just see what we get here. So I'm going to go over here and hit attach. And as you can see, let's go over, give this a refresh. That did actually work. So it did actually go ahead and attach that with the ID.
07:15
Now, there's a couple of issues with this. The first thing is we're not validating anything. So validation here would be a good idea to make sure the topic actually exists before we go ahead and attach it. The second thing that you can do is you can go ahead and do a topic find or fail on that ID. So this will actually grab a topic instance and pass this in and it needs to exist.
07:37
So we've got find or fail. If an ID that's passed in does not exist within the topics table, this will just not work. But I would always recommend validating as well. So you could go ahead and create out a validate method in here, pass through our request, and then we could go ahead and say topic ID and say that this needs to exist within the topics table by the ID.
07:59
So we can add the validation rule in here. We could also look the topic up here as well if we wanted to. It's entirely up to you. But both of these should just work. So if we just give this a refresh and yet validate doesn't work because we're not within the context of a controller. But that is what you would want to do. So I'm just going to comment that out now so you can use it in your real apps.
08:19
OK, so now that we've got this done, that is actually working. So let's head back over to here. Let's go over to here. You can see that that's been attached. Eloquent Relationships, by example, is the course we're working with. Let's also attach Eloquent to that and give that a refresh. And there we go.
08:34
So although this is a really simple example that just goes to show you how you might build this up in your own apps. As long as you have a list of the things that you potentially want to attach, like topics, and you submit this through to somewhere where you're validating and then attaching that topic, then everything should work nicely. So now that we know how to attach topics, let's head over into the next episode and look at how we remove topics or related models.
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!

Episode discussion

No comments, yet. Be the first!