Use Route Model Binding for Cleaner Controllers
December 09, 2025
•
1 min read
•
79 views
Instead of manually fetching models in your controller, Laravel can do it automatically:
Before (Manual Query)
public function show($id)
{
$post = Post::findOrFail($id);
return view('posts.show', compact('post'));
}
After (Route Model Binding)
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
Laravel automatically finds the Post by ID and returns 404 if not found. You can also use custom keys:
// In your model
public function getRouteKeyName()
{
return 'slug';
}
Now /posts/my-post-slug will automatically resolve using the slug column.
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!