Back to Blog
Lesson 32 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

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.

LaravelCRUDroutingEloquentbeginnerphpbackend

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.

HTML
style="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.

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.

PHP
public 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:

PHP
Route::delete('/tasks/{task}', [TasksController::class, 'destroy'])->name('tasks.destroy');

Now, update your controller to use the route() helper:

PHP
return 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

  1. Open your tasks/index.blade.php file.
  2. Add a "Delete" button inside the loop where you display your tasks.
  3. Ensure the form includes @csrf and @method('DELETE').
  4. Add the destroy method to your TasksController and verify that clicking the button removes the task from your database.

Common Pitfalls

  • Forgetting @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.
  • 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 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.
  • Missing Route Model Binding: If you forget to type-hint the Task model in your controller method, you'll have to manually fetch the task using Task::findOrFail($id), which is unnecessarily verbose.

Recap

We've completed the CRUD lifecycle. You now know how to:

  1. Use the @method('DELETE') directive to spoof HTTP verbs.
  2. Handle deletion logic within the Eloquent model using $task->delete().
  3. 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.

Similar Posts