How to use pre-defined destroy method in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Deletion in Laravel: Understanding Pre-defined Destroy Methods
I wonder about the best practice way of deleting an object made from a model? I have seen several similar questions but no one touching on the whole subject nor my specifics further down.
When working with Laravel, especially when dealing with resource management through controllers, it's natural to question why we pass entire Eloquent models rather than just their primary keys. This confusion often stems from not fully grasping a core feature of the framework: Route Model Binding (RMB).
This post will walk you through exactly how to use Laravel’s pre-defined destroy method, demystifying the process and showing you the best way to handle object deletion in your application.
The Concept: Route Model Binding Explained
The reason your controller method receives an entire $post object instead of just an ID is due to how Laravel streamlines data retrieval using Route Model Binding. Instead of manually querying the database inside your controller (e.g., Post::findOrFail($id)), Laravel can automatically resolve the model instance based on the route definition.
When you define a route like /posts/{post}, Laravel inspects the {post} segment and attempts to find the corresponding Eloquent model in the database. If successful, it injects that complete model object directly into your method signature. This saves you boilerplate code and ensures that the object you are working with is already validated and loaded. This efficiency is a hallmark of well-designed frameworks like Laravel, which emphasizes developer experience and clean code patterns (as promoted by resources on https://laravelcompany.com).
Step-by-Step Implementation for Deletion
To successfully implement a delete feature using the pre-defined method, we need to coordinate three components: the view (HTML), the routing (web.php), and the controller logic.
1. HTML: Creating the Delete Form
The first step is presenting the deletion action to the user via an HTML form. This form must use the POST method and include a CSRF token, which Laravel handles automatically when interacting with its built-in methods.
<!-- resources/views/posts/create.blade.php (or similar view) -->
<form method="POST" action="{{ route('posts.destroy', $post->id) }}">
@csrf
<button type="submit" class="btn btn-danger">Delete Post</button>
</form>
Notice that the action attribute points to a named route, which we will define next.
2. web.php: Defining the Route (The Magic of Binding)
In your routes file, you define the route using Model Binding syntax. This tells Laravel: "When someone hits this URL, find the correct Post model and pass it to the controller method."
// routes/web.php
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class); // This automatically registers destroy, create, store, etc.
// Alternatively, for a custom route structure:
Route::delete('/posts/{post}', [PostController::class, 'destroy'])->name('posts.destroy');
By using the Route::resource() helper, you instantly get all necessary methods, including the destroy method. The {post} segment here is crucial; it signals to Laravel that a model instance should be bound to this parameter before executing the controller action.
3. PostController: Executing the Destroy Method
This is where we leverage the magic. Because we defined the route structure correctly, the destroy method automatically receives the fully hydrated Post object as an argument.
// app/Http/Controllers/PostController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post; // Make sure your model is imported
class PostController extends Controller
{
/**
* Remove the specified resource from storage.
*
* @param \App\Post $post The Eloquent model instance found via Route Model Binding.
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
// Because Laravel has already loaded the model, we only need to call delete()
$post->delete();
// Redirect the user after successful deletion
return redirect()->route('posts.index')->with('success', 'Post successfully deleted!');
}
}
In this example, the type hint Post $post tells PHP and Laravel that this method expects an instance of the Post model. When a request hits the route /posts/{post} (assuming the route is set up correctly), Laravel automatically fetches the post from the database and passes it into this method as the $post variable. This makes your controller code incredibly clean, secure, and highly readable.
Conclusion
Using pre-defined methods like destroy combined with Route Model Binding is a powerful pattern in Laravel development. It shifts the responsibility of finding the resource from your controller logic to the routing layer, resulting in cleaner, more robust, and significantly less error-prone code. By understanding how models are bound to routes, you unlock a much more efficient way to manage CRUD operations. Keep exploring the power of Eloquent and the framework—it truly makes complex tasks manageable!