Resource Controllers
Resource controllers give you the seven standard CRUD methods at once.
Syntax
Route::resource('posts', PostController::class);A resource controller implements the seven conventional actions for a resource: index, create, store, show, edit, update and destroy. Register them all with a single route line.
Example
use Illuminate\Support\Facades\Route;
// This one line registers 7 RESTful routes:
Route::resource('posts', PostController::class);When to use it
- A blog platform registers all CRUD operations for Post with one Route::resource('posts', PostController::class) line, generating seven named routes automatically.
- An API that follows REST conventions uses Route::apiResource() (which skips create and edit HTML form routes) for its PostController.
- A developer limits a resource controller to only index and show with ->only(['index','show']) to expose a read-only public API for products.
More examples
Generate a resource controller
The --resource and --model flags generate all seven method stubs with the Post model already type-hinted in each signature.
php artisan make:controller PostController --resource --model=Post
# Creates the controller and injects type-hinted Post in each methodResource controller method map
Lists all seven resource actions alongside their HTTP verb and URL, demonstrating how they map to the route convention.
class PostController extends Controller
{
public function index(): View { /* GET /posts */ }
public function create(): View { /* GET /posts/create */ }
public function store(Request $r): Redirect { /* POST /posts */ }
public function show(Post $post): View { /* GET /posts/{post} */ }
public function edit(Post $post): View { /* GET /posts/{post}/edit */ }
public function update(Request $r, Post $post): Redirect { /* PUT */ }
public function destroy(Post $post): Redirect { /* DELETE */ }
}Scoped and API resource routes
Scoped resource binding ensures the nested {post} belongs to the {user}, while apiResource() omits the two HTML form routes not needed by APIs.
// Nest resources: /users/{user}/posts/{post}
Route::resource('users.posts', UserPostController::class)
->scoped(['post' => 'slug']);
// API resource (no create/edit HTML form routes)
Route::apiResource('products', ProductApiController::class);
Discussion