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.