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
09. Two ways to insert

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 our relationship defined,
00:02
we're going to take a look at two different ways that we can insert data for a user's posts. So the first way that we're going to look at is probably the way that you're going to do this 99%
00:14
of the time. But there is a chance that you might want to create a model or make a model before you insert it. And I'll tell you why that is when we get to it.
00:23
So the first thing that we'll do is just create out a new route for this so we can see what's going on. And we'll just call this Insert or Create. And let's call this Create, actually,
00:32
because that makes a little bit more sense. And now we're going to go ahead and just say User. Remember, exactly like we did with the one-to-one, we access the method now.
00:41
So we end up with something, the actual relationship that we can then insert on. And then we're going to call the Create method. And into here, we can just pass an array
00:49
with all of the post information. And what will happen behind the scenes with Eloquent is it will automatically fill in the user ID for you because we're accessing this relationship via this user.
01:00
Now, at the moment, this isn't going to work because remember over in our address model earlier, we need to add a fillable field inside of here, a fillable column, to say that we can fill the body in.
01:11
We don't need to do that for user ID because that's done behind the scenes, but we're going to add in body to fillable. So now we can just add in a body here.
01:19
So let's say a first post, and let's go ahead and run this and see what happens. So let's head over to our Create endpoint here and hop over to the database directly, and there we go.
01:31
So as you can see, it works exactly the same. It's a one-to-one, but the relationship type now has changed. So if we create a second post here and give that a refresh, we can return to how we access this
01:44
and we'll see we get a collection. We don't get the first one, we get all of these because it's one to many. So if we head back over to our homepage here,
01:53
our collection now has two items. And don't worry too much about everything outside of here. We don't need to directly access this items property. We'll look at working with collections
02:04
when we dump these out to a page, but you can see when you do die and dump this, you get an array here internally within this collection with two posts.
02:12
And these look exactly as you would expect. They have all the data that you can read from them. So that's one way to insert these and a quick preview of how we see these.
02:22
In the next episode, we're gonna take a look at how we can iterate over these and access this data. So the second way to create an item like this really depends on if you need to do something to the post
02:35
or model that you're inserting before you insert it. So let's go ahead and just duplicate this route so we have a reference to both. Let's call this alternative.
02:44
And this will involve not passing an array directly through to create, it will involve creating the post before we go ahead and insert it. That doesn't mean that we're going to persist this post
02:56
to the database because if we were to create a post out first and then attach it to a user, the user ID wouldn't be filled and this is a required column in our database.
03:07
So what we're gonna do here instead is we're gonna use a make method on this post. So let's make sure we pull the post in at the top of our page, the namespace,
03:18
and then we're gonna pass the data in that we wanna see. So let's say body and a third post. And now what do we do? Well, let's go ahead and just say,
03:28
well, let's pass this post into create and just see what happens because that would kind of make logical sense. So let's come over to create and alt and see what we get.
03:38
Okay, so the method create on the has one or many relationship type needs to be an array. So obviously we can't do this.
03:47
We saw us passing an array into here before. Now, the whole point of doing this is if on this post, you need to do things to this post before you go ahead and insert it,
03:58
this is when you're gonna need to use this type of insertion but we can't use create here. What we need to do is use save. So we've already got a post
04:08
that isn't persisted at the database level just yet but then we wanna save this to the user's post now that it exists. So this will take in a model that we can then use.
04:18
So if we give this a refresh now, notice that that has worked. And if we head over to the database, sure enough, it is in there.
04:24
Now, let's just talk about the reason why we might need to do this before. Now, when we make up a model, we might have methods over on this model
04:33
that we need to do something to. We might even have relationships on this post that we need to insert before we save it to a user. I'm not gonna go too much into detail on this.
04:45
This is something that you will find that you will come back to if you do need to do this at any point, if you find things starting to get a little bit tricky
04:53
but this is another way that we can insert this in. So majority of the time, I'd say 99% of the time, you would just do this, very, very simple. But if you did need to build the post up,
05:02
modify anything about this post before you then went and inserted it into that post relationship, then this is what you would do.
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.