Challenge 2: Write a Resource Controller — Possible Solution ==================================================================== # Generate with the --resource flag php artisan make:controller ProductController --resource --model=Product // app/Http/Controllers/ProductController.php namespace App\Http\Controllers; use App\Models\Product; use Illuminate\Http\Request; class ProductController extends Controller { public function index() { return 'All products'; } public function create() { return 'Show the create-product form'; } public function store(Request $request) { // ... validate and save ... return redirect()->route('products.index'); } public function show(Product $product) { return "Showing product: {$product->name}"; } public function edit(Product $product) { return "Editing product: {$product->name}"; } public function update(Request $request, Product $product) { // ... validate and update ... return redirect()->route('products.show', $product); } public function destroy(Product $product) { $product->delete(); return redirect()->route('products.index'); } } // routes/web.php use App\Http\Controllers\ProductController; Route::resource('products', ProductController::class); WHY THIS WORKS -------------- - --resource generates a stub method for all seven RESTful actions (index, create, store, show, edit, update, destroy) already correctly named and shaped to match exactly what Route::resource() expects — no method names need to be guessed or looked up. - The --model=Product flag additionally pre-fills the show/edit/update/ destroy method signatures with the Product type hint already in place (Product $product), setting up route model binding automatically rather than requiring it to be added by hand afterward. - Route::resource('products', ProductController::class) is the single line from Chapter 2 that ties every one of these seven methods to its matching route — the controller and the route declaration are two halves of the same pattern, and neither one alone is sufficient: the controller methods do nothing without the resource route pointing requests at them, and Route::resource() would error if the controller class (or its expected methods) didn't exist.