Finalize your Task Manager CRUD functionality by implementing secure edit and delete features. Learn how to maintain data integrity in your Laravel application.
Previously in this course, we covered Mastering Named Routes in Laravel for Maintainable Code to decouple our URLs from our view logic. In this lesson, we are finally tying the knot on our core CRUD (Create, Read, Update, Delete) cycle.
We’ve already built the foundation for displaying real database records and securing our application with user-scoped data. Now, we need to allow users to modify and remove those records while ensuring that no user can accidentally (or maliciously) touch data they don't own.
In web development, CRUD stands for Create, Read, Update, and Delete. While "Create" and "Read" are essential for showing data, "Update" and "Delete" are where the stakes get higher. When a user edits or deletes a task, we must ensure they are the owner of that task and that the incoming data is sanitized.
To edit a task, we need two components: a form to capture the new data and a controller method to persist it. Since we are using Route Model Binding, our controller action is clean and expressive.
In your TasksController, ensure your update method looks like this:
PHPpublic function update(Request $request, Task $task) { #6A9955">// Authorization: Ensure the user owns the task $this->authorize('update', $task); $validated = $request->validate([ 'title' => 'required|max:255', 'description' => 'nullable|string', ]); $task->update($validated); return redirect()->route('tasks.index')->with('success', 'Task updated!'); }
By calling $this->authorize('update', $task), we lean on Laravel's policy system to check ownership before the database is ever touched. This is a critical step for data integrity.
Deletion is permanent, so it requires an extra layer of caution. We use the DELETE HTTP verb and a form with the @method('DELETE') directive to signal our intent to the router.
In your index.blade.php file, your delete button should look like this:
HTMLstyle="color:#808080"><style="color:#4EC9B0">form action="{{ route('tasks.destroy', $task->id) }}" method="POST"> @csrf @method('DELETE') style="color:#808080"><style="color:#4EC9B0">button type="submit" onclick="return confirm('Are you sure?')">Deletestyle="color:#808080"></style="color:#4EC9B0">button> style="color:#808080"></style="color:#4EC9B0">form>
The onclick handler provides a simple client-side safeguard, while the @method('DELETE') ensures our route matches the DELETE verb defined in routes/web.php.
Route::delete('/tasks/{task}', [TasksController::class, 'destroy'])->name('tasks.destroy'); to your web.php.TasksController, add a destroy(Task $task) method that calls $task->delete() and redirects back to the index.@method('PUT') or @method('PATCH') directive to handle the update request correctly.user_id column.$fillable array in your Task model, your update() call will fail or be insecure. Check Preventing Mass Assignment in Laravel if you run into issues.@csrf for state-changing actions.We have successfully closed the loop on our Task Manager. By combining Route Model Binding with explicit authorization and proper HTTP verbs, we've built a robust system for managing user data. You now have a fully functional CRUD application that is secure, maintainable, and ready for more complex features.
Up next: We will dive into Database Relationships to associate our tasks with categories or projects, moving beyond flat data structures.
Master the final step of CRUD by learning to delete records safely in Laravel. We cover DELETE requests, route naming, and Eloquent deletion.
Read moreLearn 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.
Task Manager: Completing CRUD Functionality
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