Adding Delete Project Form 0:00Alright, let's review the homework solution from the last episode. So I noted that when you edit an existing project, we need some option to delete it entirely. I don't want to update it, I want to delete it. Okay, so we have our form to update the project, let's make another form to delete it. So let's just grab all of that right here, I'll paste it in, we'll say delete project, and we don't want it blue, so I'll get rid of that. Okay so if we come back and give it a refresh, alright, we of course need a little bit of space there. You know what, I don't know if Bulma has utility helpers, so bear with me, but we're just goingspace there. You know what, I don't know if Bulma has utility helpers, so bear with me, but we're just going to do some margin bottom here. Okay, good enough. So if I hit this button, it will correspond to this form, and if I hit this button, it will be a different form altogether. And that way we can have two different endpoints. So let's think about it. If I click on delete project, well, I want to submit a delete request. So I could say method field is delete, and I'll actually show you a different way to Wiring Delete Route 0:57If I click on delete project, well, I want to submit a delete request. So I could say method field is delete, and I'll actually show you a different way to prepare this in just a moment, but let's get it working first. And then we'll say, well, we're going to default to post, right, and we learned about this in the last episode. But we're going to secretly tell Laravel what I really want is a delete request, even though the browser doesn't fully support it. And then the action will be to the correct endpoint to delete a project. Let's run phpArtisanRouteList again, and you can see a delete request to this URI. Okay, slash projects slash project ID.Let's run phpArtisanRouteList again, and you can see a delete request to this URI. Okay, slash projects slash project ID. Now don't forget, we also need that CSRF field, so we'll add that as well. So let's think about it. When we submit this form, it's going to hit this endpoint, and it will send us to the projects controller in a method called destroy. So like we did before, let's find that method, and all I'm going to do here is die and dub hello once again to prove that we are hitting this method. Okay, let's give it a shot. Delete, and there we go. Deleting Project Record 2:02Okay, let's give it a shot. Delete, and there we go. So now we need to know which project are we deleting. Well, we already know that this section is a wildcard, which means, like the other methods, we can accept it here. Delete the ID. Okay, refresh. All right, delete the record with an ID of 1. All right, well, we can just say project, find by the given ID, and delete it. All right, now it's out of the database.All right, well, we can just say project, find by the given ID, and delete it. All right, now it's out of the database. Why don't we redirect back to the projects overview? All right, so let's edit this one more time, delete, and now that redesigned project has been deleted. Let's delete rebranding, so I think that's a project with an ID of 2. All right, delete it, and now it's gone. So I hope that makes sense, and often I've found with newcomers that can be a little confusing because you think, well, I just want a link to hit that method. How do I just add an anchor tag that says delete? Why Deletion Needs Form 2:54confusing because you think, well, I just want a link to hit that method. How do I just add an anchor tag that says delete? And when you click on it, it takes you to the necessary place to delete the project. You can't do that easily. You need to submit a delete request. Otherwise with an anchor tag, unless you add some JavaScript on top of it, which you can absolutely do, but otherwise with basic HTML, an anchor tag is going to do a get request. And those, of course, should always be idempotent requests. Now what else? A quick note here. Using Blade Form Directives 3:23Now what else? A quick note here. There are blade helpers for these sorts of things because they're so common, and you'll do it for every single form. So instead, I could say method, delete, and CSRF, and those are blade directives that are just going to expand to the same thing. So this will be exactly the same, but a few less keystrokes. So we'll come back. Let's edit the project with an ID of 3. View the source, and if I scroll down, you still get your hidden delete input as well Handling Missing Projects 3:47Let's edit the project with an ID of 3. View the source, and if I scroll down, you still get your hidden delete input as well as the token. So that means I can do this at the very top as well. And I'll just update this to be a patch request. Useful. So now, before we finish up, let me show you one last thing. Let's find a method. How about right here? So when we edit a page, we hit this controller here, where it tries to find the project andHow about right here? So when we edit a page, we hit this controller here, where it tries to find the project and then it loads a view. But now, and we touched on this I think one episode ago, what if you give us a project that we know doesn't exist in the database? Well, it blows up because we tried to find that project and we just assumed one would exist, but it doesn't in this case. So it blows up. We can tweak find to be find or fail. It's a slight variant.We can tweak find to be find or fail. It's a slight variant. But now if I give it a refresh, we get a proper 404 page. So this is our way of saying try to find the project with an idea of whatever you gave us. But if it doesn't exist in the database, something's probably going on. So let's throw an exception, Laravel will catch it behind the scenes, and then present a 404 to the user. And in general, in most places, you can stick to that. So let's update all of these like so.And in general, in most places, you can stick to that. So let's update all of these like so. Okay, so now in the next episode, I want to focus on how we can clean up this controller a bit more. Because things like this, this is a long form way to do it. But as it turns out, there are some cleaner and faster ways to update a project or to fetch a project. So we'll talk about that in the next episode.