Playing
04. Updating 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
So let's now look at updating one-to-one relationships. And once again, we're going to do this
00:05
within a separate route, just so we can grasp the concept of this before we later go ahead and build our real-world application. So I'm going to go ahead and create out another get route
00:16
here, which would normally be something like put or patch. And we're just going to call this update. Now, inside of here, because we're working with a one-to-one relationship, which,
00:26
to be honest, is pretty rare when you're building an application. We'll talk about that a little bit later. But we want to access that address relationship
00:35
and just update the data. And that will always update the one thing that a user has. Let's demo this out and see what we mean. So once again, because we're not authenticating anywhere,
00:45
we're going to go ahead and find that particular user whose address we want to update. And then we're going to just do the same thing. We're going to access the address relationship
00:54
by its method. And you guessed it, we're going to call the update method. And then we can just pass through any of the specific fields that we want to update.
01:03
We can choose to update one of these columns. We can update all of them at the same time. It really doesn't matter. So we're going to go ahead and update the line 1
01:12
here to be something different. Let's say one eloquent lane, for example. And let's go ahead and run this and see what happens. So obviously, at the moment in the database,
01:22
it's set to 38 code road. Let's head over to the update endpoint we've just created. And then let's head over to the database. And there we go.
01:30
Sure enough, that has been updated. And you'll notice that the updated at date will be changed as well, because we have updated this. This is automatically touched to increment the timestamp
01:41
to the current date and time. Most of the time, we don't reference updated at, but it's a good idea to have it there just so you know when this has been updated.

Episode summary

In this episode, we're diving into how to update records in a one-to-one relationship—in this example, updating a user's address. We set this up using a separate route (though in a real app, you'd probably use a PUT or PATCH request instead of a GET). This helps isolate the concept so we can really focus on how updating works without the noise of more complex authentication or app structure.

We walk through finding the user whose address we want to update, then access the related address through its relationship method. Using the update method, we modify whichever fields we want (we change the street from '38 Code Road' to '1 Eloquent Lane' as a demonstration). After running this update, we check the database and confirm the change has taken effect. You'll see that not only has the address changed, but the updated_at timestamp is also refreshed automatically.

This is a nice, practical intro to updating related models, and we highlight how rare pure one-to-one relationships are in many apps, but it's a good building block for getting comfortable with Eloquent relationships and data updates.

Episode discussion

No comments, yet. Be the first!