Playing
03. Creating an address

Episodes

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

Transcript

00:00
At the end of this section, we're going to be building out a real world example of how to create an address, update an address, delete an address.
00:08
But for now, what we're going to do is just create a couple of routes over the next few episodes just to get the hang of creating, updating and deleting relationships. So the first thing that we're going to do is create out a new route in here. It's just going to be a get route. Normally, we would post through to create an address, but let's create out a create page in our app or a route in our app so we can create an address for a user.
00:33
Before we do that, let's just make sure that this particular user doesn't have any addresses associated with them. And we'll go ahead and fetch that user out of the database to go ahead and create that relationship. Now, there are lots of different ways that you can create relationships within Laravel. You can manually insert the user ID when you're creating an address.
00:54
I'll give you an example of that in a minute. But let's look at the more traditional approach that we would take here. So we know that we have an address relationship. So what we can do now is access the address relationship, but by its method.
01:10
So now that we are inserting or creating a record, we want to access the method. What we can then do is chain onto this the create method, and then inside of this create method, we can give an array with the data that we want to fill. So we know that we have a line one in here. So let's go ahead and add in 38 code road.
01:32
And then when we run this, hopefully this goes ahead and gets created. Now, I know it's not actually going to work at the moment, and we're going to dive into why in just a second. So let's go over to our home page. Of course, we don't have anything there because the user doesn't have an address at the moment, but we're going to head over to create, hit enter on this, and you can see that we've got an error.
01:53
Now, the reason that this happens is when you are creating and updating things within Laravel, we are automatically protected against mass assignment, basically accidentally mass updating records and their properties. So what we need to do is head over to our address model, which we've not even looked at yet because we haven't really needed to add anything to this. And we want to go ahead and add in a fillable property to this and tell Laravel that this model can update these particular fields. You might have columns in your data space that you don't want to be updated, in which case you would exclude these from this fillable property.
02:31
And we saw that over in the user model where we have a fillable property with name, email, and password. Of course, these three things need to be added when we are creating a user. So we're going to do the same thing inside of our address model, and we're going to create out a fillable property in here. And of course, we're going to add in that line one.
02:54
There we go. That's it. So let's head over, refresh this, and sure enough, that worked. Let's go over to our database, give this a refresh, and there we go.
03:03
So we haven't had to, of course, manually create this in the database now. This has been assigned to this user because when we access this relationship via this user, Laravel will automatically fill this in for us. We haven't had to add in the user ID of the user manually, and that wouldn't work anyway because user ID is not fillable. So there we go.
03:26
We have created out a address for that particular user. Now, another way of doing this if you don't have access to the user and you just know the user's ID is to do something like this, and it's not something I would recommend. Let's go ahead and just comment out what we've done already, and let's come down here. So we're going to go ahead and say address create.
03:48
So we're basically creating an address from scratch here. Again, we're going to pull the namespace of address just up the top here, and we're going to go ahead and pass in the data that we need. So if we were to pass in line one, let's say 38 code lane, if we were to create this, what do you think would happen? Well, let's go over and give this a try.
04:10
So it's telling us that we don't have a user ID in here. So user ID needs to be in this address because addresses always belong to users. So if you were creating this manually, you would have to pass a user ID in like this. Now, again, that's not going to work because we can't fill this.
04:27
It's not a fillable field. If for any reason you have to do this, then over on your address model, you would have to add user ID as a fillable field. There's another way to get around this, which I'll show you in a second. If we give this a refresh, sure enough, that works, and we have now created another address for that user.
04:45
Now, if you ever needed to do this and you didn't want to add user ID to fillable, and this is a sort of rare case where you'd want to force create a model, you can use force create. That will ignore any of the fillable fields that you have defined, and it will create this out for you. So if we come over here and give that a refresh, you can see that despite the fact that over on the address,
05:06
we don't have user ID as a fillable field, that goes ahead and creates it anyway. Now, it's a good idea to learn about this stuff, but in most cases, we are going to have, and if I just comment that out so we have it as a reference, we most of the time would do something like this. If you have a user who is authenticated in your application like this somewhere,
05:29
you would always create the relationship via the relationship you have defined on that particular model. It's a much safer way of doing things, and it's a little bit cleaner as well. So there we go. We have created out an address for that user, and of course, now that we have done that, it is being shown over here.
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!