How to Use Route::delete in Laravel for HTTP DELETE Requests
In Laravel, use
Route::delete to define a route that responds to HTTP DELETE requests, typically for deleting resources. You specify the URL pattern and the controller method or closure that handles the delete action. This helps you follow RESTful conventions for resource management.Syntax
The Route::delete method defines a route that listens for HTTP DELETE requests. It takes two main arguments: the URL pattern and the action (a controller method or closure) to execute when the route is matched.
- URL pattern: The path that triggers the route, e.g.,
/posts/{id}. - Action: The function or controller method that handles the request.
php
Route::delete('/resource/{id}', [ResourceController::class, 'destroy']);
Example
This example shows how to define a DELETE route to remove a post by its ID using a controller method. When a DELETE request is sent to /posts/5, the destroy method in PostController will be called to delete the post with ID 5.
php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PostController; Route::delete('/posts/{id}', [PostController::class, 'destroy']); // In PostController.php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class PostController extends Controller { public function destroy($id) { $post = Post::findOrFail($id); $post->delete(); return response()->json(['message' => 'Post deleted successfully']); } }
Output
{"message":"Post deleted successfully"}
Common Pitfalls
Common mistakes when using Route::delete include:
- Not sending the request as a DELETE method (e.g., using GET or POST instead).
- Missing CSRF token in forms when using DELETE via HTML forms.
- Not defining the route correctly or forgetting to import the controller.
Laravel requires forms to spoof DELETE requests using a hidden input because HTML forms only support GET and POST.
php
<?php // Wrong: Using GET instead of DELETE Route::get('/posts/{id}', [PostController::class, 'destroy']); // Right: Use DELETE method Route::delete('/posts/{id}', [PostController::class, 'destroy']); // In Blade template, spoof DELETE method in form ?><form action="/posts/{{ $post->id }}" method="POST"> @csrf @method('DELETE') <button type="submit">Delete Post</button> </form><?php
Quick Reference
Use Route::delete to handle HTTP DELETE requests for resource deletion. Remember to:
- Define the route with the URL and controller action.
- Use
@method('DELETE')in HTML forms to spoof the DELETE method. - Send requests with the DELETE HTTP method (e.g., via JavaScript fetch or forms).
| Concept | Description |
|---|---|
| Route::delete | Defines a route for HTTP DELETE requests |
| URL pattern | The path that triggers the delete action, e.g., /posts/{id} |
| Action | Controller method or closure handling the delete |
| Form method spoofing | Use @method('DELETE') in Blade forms |
| CSRF token | Include @csrf in forms to protect against CSRF attacks |
Key Takeaways
Use Route::delete to define routes that respond to HTTP DELETE requests for resource deletion.
Always spoof DELETE method in HTML forms using @method('DELETE') and include @csrf for security.
Ensure the client sends the request with the DELETE HTTP method, not GET or POST.
Import controllers properly and define the destroy method to handle deletion logic.
Follow RESTful conventions by using Route::delete for deleting resources.