From the course: Building a Website with Laravel, React.js, and Inertia

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Deleting a post

Deleting a post

- [Instructor] The final piece of functionality we will implement is deleting posts. We already have a button for it that we will use. Currently, it doesn't do anything at all, so let's add an on click handler. That will trigger a handle delete post function, which will send a delete request with Axios. Let's build it together. In the post index component. Let's add an open function to the button that will call a handle delete post method. We'll pass in the post ID. Okay, onto the handle delete post method. I'll paste the code in and we will go over it together again. In here, we first ask the user if they really want to delete the post. We implement a simple confirmation alert to save time, but you could pull a confirmation module instead. Then we make a delete request with Axios to the API endpoint and pass in the post ID. Finally, we update the local state by removing the deleted post. All this disrupt inside a try-catch block to catch any potential errors. Let's try it out. I'm…

Contents