Laravel From Scratch [Part 8] - Edit & Delete Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome back to liar Hill from scratch in the last video we made it so that we could create a post from our front end here and we also implemented an editor so now what I want to do is make it so we can edit and delete posts so we want to from the show page which is the individual post we want to have an edit and delete button now later on we add authentication we're going to have that as well as a dashboard where we can we can create posts art I'm sorry edit and delete posts so let's go to our show view show Glade PHP and I'm going to go under see where I want to put this let's go under the small here and we'll put another HR and then we'll put a link okay and this is just going to go this is going to go to the edit view so let's say href equals slash posts slash and then the ID so we can just go like that post ID and then slash edit we'll give this a class-b TN and BTN default okay and that's going to say edit so we'll save now if we reload we have an edit button and that goes to post slash the ID and then edit so let's go to our controller and go to the edit function and we want to bring in that that post which we can do we using fine just like we did in the show view and then we just want to load up the edit view so it'll be post dot edit okay we're going to pass along the post as well so let's go to our views and then posts and we'll create a new file called edit blade dot PHP okay and we're going to copy what's in the create template because it's basically the same thing we just need that form and we'll change the heading to edit post and the action is now going to go to controller update okay now we also want to include the ID so to do that we're going to make this an array so we're going to put brackets there in there and then we're going to put a comma and say post ID okay so it knows which which one it's updating now if we submit this as is it's going to make a post request to update and if we look at our routes let's clear this out and then we'll do PHP artisan route list and if we look at the update which is right here we're making if we well it's going to post slash and then the ID so if we mean if if we do a post request to that it's going to actually is that where yeah it's going to go update our post slash ID so right here is what we want and that has to be either a put or a patch request alright there actually isn't a route to make a post request to this right here so what we need to do is close this up we can't change this to put we can't do something like that you can either do get or post but what laravel allows us to do is spoof up put request and we can do that by simply adding a hidden input write up break down here we'll say form hidden and all we have to do is pass in underscore method and then for the second parameter we're going to put the type of requests we want to make which will be a put request okay that way it'll it'll do what we needed to do so let's save actually one more thing we need to add the values so right here for the title instead of being blank we want it to be post title and then right here should be post body so now if we go back and click Edit takes us to the forum and it's prefilled with our values now if we submit it's going to go to the update function which is blank so if we go to our post controller we want to go to this update so I'm going to copy what we have in the store which is this here we want the validation and all this stuff and then we'll place that an update and all we really need to change is this right here because we're not creating a new post what we're doing is we're going to find a post so we're going to say find and then just pass in ID all right and then the rest can be the same let's just change the message to post updated all right so let's go back and let's reload this and let's just remove the bold here and then save that so post updated now if we go to post for the bold is gone alright so we can now update posts now for delete we can't just simply add a link to delete it's going to be we need to make a post request and then also spoof it with a delete like we did with the put so that has to go on a forum so let's go to our show method our show pops our show view and go right under where we put the edit and we want to put a forum here alright so let's do forum actually that needs to be like that so forum open and in here we're going to pass in an array we're going to say action and we're going to set that to let's see we're going to set that to an array actually and then in here whoops here we're going to say post controller at destroy sorry about that ding and you guys and then we also want to pass the ID so it knows which one to delete so post ID alright and then we also want the method so after the array after the bracket here we'll put a comma and we're going to say method disguise it as a post even though it's going to be a delete and then let's also have a class okay so class and let's give it a class of BTN and then BTN oh no we don't want to do that here we want to do pull right the BTN will go on the submit button yeah because we want to pull it to the right we want to float it to the right of the edit alright and then we just want to end it so form close and then inside the forum we're going to have two things we're going to have the hidden spoofing method and we're going to have the submit all right so let's do form hidden okay and then that's going to take in underscore method and we want this to actually be a delete request okay and then we'll do the submit so submit is going to be going to say delete and then we'll pass in an array of parameters so we just want to put a class class will be BTN BTN - danger all right let's just let's see what that looks like what's this washing our expecting closing bracket okay what did I do Oh all right here should be another bracket there we go so now we have our delete button and that's getting submitted to the destroy function so let's go down to destroy and what we want to do here is find the post by its ID that's being passed in and then we can simply do post delete and then we'll just redirect back to posts let's say post removed okay let's try that reload and delete and now post removed so we have full crud functionality now the next thing we're going to do in the next video is we're going to add authentication so that we can register and login we can create our own posts and then we'll move on to access controls so we can only we can only manage our own posts all right so we'll get into that next
Info
Channel: Traversy Media
Views: 216,462
Rating: undefined out of 5
Keywords: laravel, laravel collective, laravel forms, laravel database
Id: PAP8IS_ak6w
Channel Id: undefined
Length: 9min 57sec (597 seconds)
Published: Sat Jun 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.