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
12. Updating records

Episodes

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

Transcript

00:00
So we're going to talk about updating posts now but we're going to keep this really short because pretty much everything we've discussed within delete is going to be exactly the same. So the
00:10
best way to do this would be to authorize this first in some way and then go ahead and just update whichever post we're getting through here. So let's just take a look at a really, really quick example and we'll build this through and look at this in more detail later on. So we're going to say update post. So we're going to pass the post through that we want to update.
00:31
We're going to get the currently authenticated user or in our case the user that we just have for testing and then we're going to go ahead and just update the post. So we're going to say post update and we can pass in a new title or a new body. Again the really important thing to think about here is that you would need to authorize this before you do this because otherwise anyone
00:52
could post or pass through any post id. So let's just take a look at a really simple example here of updating the body, a new body and let's go over and update one of these posts. So let's say let's just change this back to my user and let's just pass in the id of six under update and if we head over the database sure enough it has updated the body of this. But again you could use this
01:19
technique here if you wanted to just update this through the user who owns the post. It's entirely up to you. So let's go ahead and grab what we did here and let's say user post find post and then let's go ahead and update this with our body another new body. So let's go ahead and just comment this out so it doesn't update twice and we should be good. So let's go and hit this endpoint
01:48
again and there we go another new body. So again two different ways like we did with deleting that we can access the post that we want to delete or update and then go ahead and do this either by grabbing this through the relationship or just doing this on its own but really importantly including authorization.

Episode summary

In this episode, we dive into how to update records—specifically posts—in our application. We'll keep things concise since updating is really similar to how we've already handled deleting records in the previous video.

You'll see a quick walkthrough of the update process: First, we talk about authorizing the action to make sure only the right users can make changes. Then, we'll pass in the specific post to update, change some fields (like the title or body), and actually perform the update in the database. You'll notice, just like with delete, there are a couple of ways to find the post to update—either directly or through the user's relationship to the post.

We demo updating a post by changing its body and confirming it's been updated in the database. Finally, we emphasize the importance of adding authorization so nobody can just edit any post by guessing IDs. By the end of this video, you'll see two different methods for updating posts and understand the essential security step of authorization before making any changes.

Episode discussion

No comments, yet. Be the first!