Deleting Records: A Laravel CRUD Guide
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.
The DELETE Request Pattern
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.
Adding a Delete Button to the UI
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_methodinput field with a value ofDELETE. Laravel’s router detects this and overrides the HTTP method.
Handling the Request in the Controller
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.
Using Named Routes for Redirects
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.
Hands-on Exercise
- Open your
tasks/index.blade.phpfile. - Add a "Delete" button inside the loop where you display your tasks.
- Ensure the form includes
@csrfand@method('DELETE'). - Add the
destroymethod to yourTasksControllerand verify that clicking the button removes the task from your database.
Common Pitfalls
- Forgetting
@csrf: If you leave this out, you will receive a419 Page Expirederror. Laravel requires this token to ensure the request is coming from your own site. - Using a link instead of a form: Beginners often try to use
<a href="/tasks/1/delete">Delete</a>. This is a security risk because it triggers a destructive action via aGETrequest, which can be accidentally triggered by search engine crawlers or browser pre-fetching. Always use a form withPOST/DELETEfor destructive actions. - Missing Route Model Binding: If you forget to type-hint the
Taskmodel in your controller method, you'll have to manually fetch the task usingTask::findOrFail($id), which is unnecessarily verbose.
Recap
We've completed the CRUD lifecycle. You now know how to:
- Use the
@method('DELETE')directive to spoof HTTP verbs. - Handle deletion logic within the Eloquent model using
$task->delete(). - Future-proof your redirects by using named routes instead of hardcoded strings.
Up next: We will dive deeper into Using Named Routes to clean up our entire application's navigation structure.
Work with me

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.