Master the final step of CRUD by learning to delete records safely in Laravel. We cover DELETE requests, route naming, and Eloquent deletion.
Previously in this course, we covered Updating Existing Records: A Laravel CRUD Guide. Now that you can create, read, and update tasks, the final piece of the puzzle is removing them. This lesson focuses on the "D" in CRUD: safely deleting records from your database.
In web development, we use different HTTP verbs to signify the intent of a request. While GET retrieves data and POST submits it, DELETE is the semantic standard for removing a resource.
Because standard HTML <form> tags only support GET and POST, Laravel uses a "spoofing" technique. By including a hidden field in your form, you tell Laravel to treat the request as a DELETE action.
To delete a record, we need a form that sends a DELETE request to a specific route. We'll add this to our task list view.
HTMLstyle="color:#808080"><style="color:#4EC9B0">form action="/tasks/{{ $task->id }}" method="POST"> @csrf @method('DELETE') style="color:#808080"><style="color:#4EC9B0">button type="submit">Delete Taskstyle="color:#808080"></style="color:#4EC9B0">button> style="color:#808080"></style="color:#4EC9B0">form>
Here is what is happening under the hood:
@csrf: This directive prevents Cross-Site Request Forgery, which we explored in our lesson on Understanding CSRF Protection.@method('DELETE'): This Blade directive adds a hidden _method input field with a value of DELETE. Laravel’s router detects this and overrides the HTTP method.Now that the request is properly formatted, we need to handle it in our TasksController. We’ll use Introduction to Route Model Binding in Laravel to inject the specific task instance automatically.
PHPpublic function destroy(Task $task) { $task->delete(); return redirect('/tasks')->with('success', 'Task deleted successfully!'); }
The $task->delete() method is an Eloquent helper that executes the SQL DELETE statement for that specific record. After the deletion, we redirect the user back to the list.
Hardcoding URLs like '/tasks' inside your controllers is a common maintenance headache. If you ever decide to change your URL structure, you’ll have to hunt down every redirect in your codebase.
Instead, we use named routes. First, update your routes/web.php:
PHPRoute::delete('/tasks/{task}', [TasksController::class, 'destroy'])->name('tasks.destroy');
Now, update your controller to use the route() helper:
PHPreturn redirect(route('tasks.destroy'))->with('success', 'Task deleted!');
This way, if you change the URL from /tasks to /my-todos, the redirect will automatically stay updated because it references the route name, not the path.
tasks/index.blade.php file.@csrf and @method('DELETE').destroy method to your TasksController and verify that clicking the button removes the task from your database.@csrf: If you leave this out, you will receive a 419 Page Expired error. Laravel requires this token to ensure the request is coming from your own site.<a href="/tasks/1/delete">Delete</a>. This is a security risk because it triggers a destructive action via a GET request, which can be accidentally triggered by search engine crawlers or browser pre-fetching. Always use a form with POST/DELETE for destructive actions.Task model in your controller method, you'll have to manually fetch the task using Task::findOrFail($id), which is unnecessarily verbose.We've completed the CRUD lifecycle. You now know how to:
@method('DELETE') directive to spoof HTTP verbs.$task->delete().Up next: We will dive deeper into Using Named Routes to clean up our entire application's navigation structure.
Finalize your Task Manager CRUD functionality by implementing secure edit and delete features. Learn how to maintain data integrity in your Laravel application.
Read moreMaster updating existing records in Laravel. Learn how to bind Eloquent models to edit forms, handle PUT requests, and persist changes to your database.
Deleting Records
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation