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.
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.