Todo Laravel Project - Handling delete request

Laravel Project

← Prev

In this Laravel project tutorial we will learn to handle delete request.

In the previous tutorial we created the delete action button. If the user click the delete button then a DELETE request will be set to the delete route.

Let's go ahead and wire the delete route with the TodoController to handle the delete request.

The deleteTodoById method

Open the TodoController.php file which is inside the app/Http/Controllers directory and create a new method deleteTodoById that will handle the delete request.

public function deleteTodoById($id, Request $request)
{
  // find task
  $todo = Todo::find($id);

  // delete
  $todo->delete();
}

So, when the delete button is clicked, the deleteTodoById method of the TodoController class will be called and the {id} will be passed as a parameter.

Configure the route

Open the routes/web.php file and configure the delete route.

Route::delete('/todo/{id}', 'TodoController@deleteTodoById');

If we now visit the home page and click on the delete button the todo tasks will be removed from the database.

End

Alright, this is the end of this todo-laravel project.

I hope you enjoyed it. And I hope this was helpful to you.

You can find the complete code of this project in my GitHub repository todo-laravel. Feel free to download the code and experiment.

I wish you all the best for your future adventure.

See you again in the next tutorial. Have fun :)

← Prev

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT