Resource Controllers and Route Model Binding
Most controllers in a typical Laravel application end up handling the same predictable set of actions: listing records, showing one, creating, updating, and deleting. Laravel packages this pattern into resource controllers, paired with route model binding to remove even more repetitive code.
Resource Controllers
Instead of manually defining a route for every CRUD action, a single line can register all of them at once, mapped to conventionally named controller methods. This keeps route files short while still following RESTful conventions that other developers immediately recognize.
| HTTP Verb | Action | Route |
|---|---|---|
| GET | index (list all) | /posts |
| GET | create (show form) | /posts/create |
| POST | store (save new) | /posts |
| GET | show (view one) | /posts/{post} |
| GET | edit (show edit form) | /posts/{post}/edit |
| PUT/PATCH | update (save changes) | /posts/{post} |
| DELETE | destroy (remove) | /posts/{post} |
Route Model Binding
Normally, a controller method receiving an ID has to manually look up the matching database record before doing anything useful. Route model binding removes that step entirely: Laravel inspects the type-hinted model in the method signature and automatically injects the correct record, or returns a 404 if it doesn't exist.
- Eliminates repetitive "find or fail" lookup code in every method
- Keeps controller methods focused on actual business logic
- Supports binding by custom fields, such as a slug instead of an ID
With controllers returning data reliably, the next step is learning how to actually display that data to users — starting with Blade, Laravel's built-in templating engine.
Generating and Registering a Resource Controller
Artisan can create the controller with all seven methods already stubbed out:
php artisan make:controller PostController --resource --model=Post
Then a single line in routes/web.php registers every route from the table above:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
Run php artisan route:list to see the generated routes along with their names, such as posts.index and posts.update. Using these names in redirects and links, for example route('posts.show', $post), keeps code working even if URLs change later.
For a JSON API, there is no need for forms, so use Route::apiResource('posts', PostController::class);, which leaves out create and edit. You can also limit routes with ->only(['index', 'show']) or ->except(['destroy']).
Worked Example: Before and After Binding
Without route model binding, every method repeats the lookup:
public function show($id)
{
$post = Post::findOrFail($id);
return view('posts.show', ['post' => $post]);
}
With binding, the type hint does that work. Laravel reads the value from the URL, runs the equivalent of findOrFail on the primary key, and hands the loaded model to your method. If no record matches, the request stops with a 404 before your code runs. The parameter name must match the route segment, which is {post} here:
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
public function update(UpdatePostRequest $request, Post $post)
{
$post->update($request->validated());
return redirect()->route('posts.show', $post);
}
Custom Keys, Scoped and Nested Resources
To look records up by slug instead of ID for a single route, add the key to the segment:
Route::get('/blog/{post:slug}', [PostController::class, 'show']);
To make slugs the default for a model everywhere, override getRouteKeyName() in the model and return 'slug'.
Nested resources express parent and child relationships, such as comments belonging to a post:
Route::resource('posts.comments', CommentController::class)->scoped();
Scoping makes Laravel check that the comment actually belongs to the given post, so /posts/1/comments/99 returns a 404 if comment 99 is attached to a different post. To include soft-deleted models in a binding, chain ->withTrashed() on the route.
Common Mistakes
- Mismatched names. If the route says
{post}but the method usesPost $article, Laravel injects an empty model instead of the record. - Route order. A custom route like
/posts/archiveddefined afterRoute::resourcemay be captured by theshowroute. Define custom routes first. - Assuming binding means permission. Binding only finds the record; you still need a policy check before updating or deleting it.
- Validating inside the controller repeatedly instead of using form request classes, which keep rules in one place and can be generated with
php artisan make:request UpdatePostRequest.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.