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
16. Associating 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 already know how we can create a post for a particular user,
00:03
but what if we wanted to create the post on its own and then attach a user to that post? There are circumstances where you would need to do this, and that is generally when you're building up a post and you need to do something a little bit different.
00:16
So let's take a look at how we would do this. Let's first of all, go ahead and look at how we would create out a post in the standard way directly through the user's post relationship. So we'd go ahead and find a user,
00:29
whether that's the currently authenticated user or just a user that we're plucking out of the database like this. And then we would go and access that user posts relationship and we would go ahead and create that out with any of the details we need in here.
00:47
So another post. And if we just head over here to create, that's going to go ahead and create that. When we come back here, it's going to add on another post.
00:57
Of course, with that user, now that we have that belongs to relationship. But what if we wanted to do this slightly differently and build up the post itself? Well, let's go down here and let's say create alt and let's get rid of the user.
01:14
Just for now, and let's get rid of everything, actually, and let's go ahead and create our post manually. So we can do this by saying post and make or we can say post and create. Now, we've already kind of looked at the difference between this.
01:27
Make will create this model, but it will not persist this in the database and create will just persist this in the database. So in some circumstances, you might want to go ahead and pass in the user ID. If, for example, you are building out an API that accepted the user ID in
01:44
rather than working in the context of an authenticated user. And then, of course, you would add the body in here. And let's just say yet another post. Now, in this instance, when you are attaching the user this way,
01:56
user ID would need to be a fillable field inside of your models. You would need to add this in here. Now, this is not something I ever do. So I'm not going to go ahead and use this as an example.
02:08
But this would create the post out in the way that we want. So we wouldn't even need to assign it here. Let's just try this out just to demonstrate it again. And we'll see that this works.
02:18
So let's head over to create slash alt. And let's go back to our post page. And again, we have yet another post here by that same user that we've defined here. Now, if you needed to do something like this
02:32
but didn't want to manually pass the user ID in, you can't really create a post like this because it doesn't have a user attached. What you can do, though, is you can go ahead and create the post out but not persist it using make.
02:46
Then down here, you can actually attach the user using that relationship type. And then you can go ahead and save the post to actually persist it to the database. So we make out a post regardless of who this belongs to. We have the body in here.
03:01
Then we're going to go ahead and attach the user. That's still not going to persist it to the database. It's just going to attach it to this model. And then we're going to save this in the database.
03:11
So at the moment, if we head over to create and alt, that does nothing at all. We don't have any new post in here with that data. So we're just going to say manually created post just to differentiate these. So how do we go and attach this user to this post and then persist it in the database?
03:32
Well, first thing we do is go ahead and reference the relationship of the thing that we want to attach. This doesn't have to be a user. It could be absolutely anything. And then we go ahead and use the associate method.
03:43
Let's just spell that correctly. And then we pass in the model of the thing that we want to associate with this. So in this case, it's a user model. Now, we don't have a user model in here.
03:55
This could come from the request as the currently authenticated user. It could be something that you're manually looking up. It doesn't really matter as long as you have a user model. So I'm going to go ahead and just relook the user up in the database as we've already seen by just using find on the ID.
04:13
And then we're going to go ahead and associate that user with this. You could even do this in line if you were accepting in an ID of a user and, of course, validating it. So now if we head over to this, it still doesn't do anything. We give this a refresh.
04:27
Nothing has changed. But let's go ahead and just die dump on the post just to see the difference this makes. So I'm going to go ahead and just get rid of this association and let's have a look at this. So we built up this model, which hasn't yet persisted.
04:39
So it doesn't have an ID, doesn't have a created at date or any of that stuff. It just has the data we want to insert into the database. But there's nothing much else in here. It's just a post with a body.
04:50
Now, if we go ahead and bring this association in, have a look at what happens here. We've got the attributes in here with the body. This has automatically filled the user ID in for us based on the user model we are trying to associate. And it has also gone ahead and added this relations part, which we don't really need to know about.
05:10
We can still access this, but there's not really much point and you probably would never need to do this. But you can see it's associated the correct user under this relations just here. So what that means is now that we have that data inside of that model that hasn't yet been persisted. When we go ahead and save this to the database, it's going to fill in all the correct data for us.
05:32
Now, with this, even though the user ID has got filled, we don't need to include this within fillable. It will just work. So let's give that a refresh and it's come over to the database. And there we go.
05:43
That has been created with that user in there. So as well as the standard way of just creating user via the post relationship, we can kind of do this in the opposite way round now because we have an opposite relationship type. We can create out a plain old post, attach or associate the user, and then finally save that post.
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.