Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

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.

Previous lessonUpdating Existing RecordsNext lesson Using Named Routes
Back to Blog

Similar Posts

LaravelJune 25, 20263 min read

Task Manager: Completing CRUD Functionality in Laravel

Finalize your Task Manager CRUD functionality by implementing secure edit and delete features. Learn how to maintain data integrity in your Laravel application.

Read more
LaravelJune 25, 20263 min read

Updating Existing Records: A Laravel CRUD Guide

Master updating existing records in Laravel. Learn how to bind Eloquent models to edit forms, handle PUT requests, and persist changes to your database.

Part of the course

Laravel Fundamentals: From Zero to Your First App

beginner · Lesson 32 of 52

  1. 1

    Setting Up the Local Development Environment

    4 min
  2. 2

    Installing Laravel and Exploring Directory Structure

    3 min
  3. 3

    Understanding the .env File and Configuration

    3 min
Read more
LaravelJune 25, 20263 min read

Task Manager: Displaying Real Database Records

Learn how to display database data in your Laravel Task Manager. We'll connect your Eloquent models to your Blade views to render real, dynamic tasks.

Read more
  • 4

    The Laravel Application Lifecycle

    4 min
  • 5

    Initializing the Task Manager Project

    3 min
  • 6

    Defining Basic Web Routes

    4 min
  • 7

    Using Route Parameters

    3 min
  • 8

    Creating Your First Controller

    3 min
  • 9

    Returning Responses and Redirects

    3 min
  • 10

    Task Manager: Implementing the Task List Route

    3 min
  • 11

    Introduction to Blade Templating

    3 min
  • 12

    Using Blade Layouts and Sections

    3 min
  • 13

    Implementing Blade Partials

    4 min
  • 14

    Mastering Blade Directives for Loops and Conditionals

    3 min
  • 15

    Task Manager: Building the User Interface

    3 min
  • 16

    Understanding Database Migrations

    3 min
  • 17

    Working with Eloquent Models

    3 min
  • 18

    Performing Basic CRUD Operations

    3 min
  • 19

    Seeding the Database

    3 min
  • 20

    Task Manager: Displaying Real Database Records

    3 min
  • 21

    Capturing User Input from Forms

    4 min
  • 22

    Introduction to Laravel Validation

    3 min
  • 23

    Customizing Validation Error Messages

    3 min
  • 24

    Using Form Requests for Validation

    3 min
  • 25

    Introduction to Authentication

    4 min
  • 26

    Protecting Routes with Middleware

    3 min
  • 27

    Understanding CSRF Protection

    3 min
  • 28

    Preventing Mass Assignment

    3 min
  • 29

    Task Manager: Securing the Application

    3 min
  • 30

    Introduction to Route Model Binding

    3 min
  • 31

    Updating Existing Records

    3 min
  • 32

    Deleting Records

    3 min
  • 33

    Using Named Routes

    3 min
  • 34

    Task Manager: Completing CRUD Functionality

    3 min
  • 35

    Introduction to Database Relationships

    3 min
  • 36

    Querying Related Data

    4 min
  • 37

    Handling File Uploads

    Coming soon
  • 38

    Using Flash Messages for User Feedback

    Coming soon
  • 39

    Task Manager: Adding Status and Priorities

    Coming soon
  • 40

    Introduction to Artisan Commands

    Coming soon
  • 41

    Debugging with Laravel Tinker

    Coming soon
  • 42

    Understanding Service Providers

    Coming soon
  • 43

    Using View Composers

    Coming soon
  • 44

    Task Manager: Refactoring for Clean Code

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Testing Forms and Validation

    Coming soon
  • 47

    Using Database Transactions

    Coming soon
  • 48

    Handling Global Exceptions

    Coming soon
  • 49

    Preparing for Production

    Coming soon
  • 50

    Environment Security Best Practices

    Coming soon
  • 51

    Managing Assets in Production

    Coming soon
  • 52

    Task Manager: Deployment Preparation

    Coming soon
  • View full course