Resource controllers help organize code by grouping related actions for a resource in one place. They make building web apps simpler and cleaner.
Resource controllers in Laravel
php artisan make:controller ResourceNameController --resource
This command creates a controller with methods for index, create, store, show, edit, update, and destroy.
You can register the controller routes with Route::resource('name', ResourceNameController::class);
php artisan make:controller ProductController --resource
Route::resource('products', ProductController::class);
<?php namespace App\Http\Controllers; use App\Models\Product; use Illuminate\Http\Request; class ProductController extends Controller { public function index() { return Product::all(); } public function show(Product $product) { return $product; } // Other resource methods here... }
This BookController handles all common actions for books. The routes created by Route::resource will call these methods automatically.
<?php namespace App\Http\Controllers; use App\Models\Book; use Illuminate\Http\Request; class BookController extends Controller { public function index() { return Book::all(); } public function create() { return 'Show form to create a book'; } public function store(Request $request) { $book = Book::create($request->all()); return response()->json($book, 201); } public function show(Book $book) { return $book; } public function edit(Book $book) { return 'Show form to edit book with id ' . $book->id; } public function update(Request $request, Book $book) { $book->update($request->all()); return response()->json($book); } public function destroy(Book $book) { $book->delete(); return response()->json(null, 204); } } // Route registration example: // Route::resource('books', BookController::class);
Resource controllers follow RESTful conventions, making your app predictable.
You can customize which methods to include by using only or except when registering routes.
Use route model binding to automatically get model instances in methods like show(Book $book).
Resource controllers group related actions for a resource in one controller.
Laravel can auto-generate routes for these actions with Route::resource.
This keeps your code organized and follows common web app patterns.
Practice
resource controller in Laravel?Solution
Step 1: Understand what resource controllers do
Resource controllers in Laravel group related actions like create, read, update, and delete (CRUD) for a resource in one controller class.Step 2: Compare options with this definition
Only To group all CRUD actions for a resource in one controller correctly describes this grouping of CRUD actions. Other options describe unrelated features.Final Answer:
To group all CRUD actions for a resource in one controller -> Option CQuick Check:
Resource controller = CRUD grouping [OK]
- Confusing resource controllers with migrations
- Thinking resource controllers handle views alone
- Assuming resource controllers manage authentication
PostController for the route /posts?Solution
Step 1: Recall Laravel resource route syntax
The correct syntax usesRoute::resource('uri', ControllerClass::class)to register all resource routes.Step 2: Check each option
Route::resource('posts', PostController::class); matches the correct syntax. Route::controller('posts', PostController::class); usescontrollerwhich is different. Route::get('posts', PostController::resource()); is invalid syntax. Route::resource('PostController', 'posts'); swaps parameters incorrectly.Final Answer:
Route::resource('posts', PostController::class); -> Option AQuick Check:
Resource route syntax = Route::resource(uri, Controller::class) [OK]
- Using Route::controller instead of Route::resource
- Swapping URI and controller parameters
- Calling resource() as a method on controller instance
Route::resource('photos', PhotoController::class);Which URL and HTTP method will call the
destroy method in PhotoController for photo with ID 5?Solution
Step 1: Recall resource route conventions for destroy
The destroy method is called with HTTP DELETE on the resource URI with the ID, e.g., DELETE /photos/{id}.Step 2: Match options to this pattern
DELETE /photos/5 matches DELETE /photos/5. Other options use wrong HTTP methods or incorrect URLs.Final Answer:
DELETE /photos/5 -> Option DQuick Check:
Destroy = DELETE /resource/{id} [OK]
- Using GET or POST instead of DELETE for destroy
- Adding extra path segments like /destroy or /delete
- Confusing PUT with DELETE method
Route::resource('users', UserController::class);Which of the following code snippets will cause a runtime error when trying to access the
show method?Solution
Step 1: Understand show method parameter requirements
The show method expects either an ID parameter or a model instance via route model binding.Step 2: Analyze each option
public function show() { return 'User not found'; } has no parameter, so Laravel cannot pass the required ID or model, causing a runtime error. Others have correct parameters.Final Answer:
public function show() { return 'User not found'; } -> Option AQuick Check:
Show method must accept ID or model parameter [OK]
- Omitting parameters in show method
- Using wrong parameter types without binding
- Ignoring route model binding requirements
Product but only want to generate routes for index, show, and store methods. Which is the correct way to register this in routes/web.php?Solution
Step 1: Recall how to limit resource routes
Laravel allows limiting routes using theonlymethod with an array of method names.Step 2: Evaluate each option
A usesexcept(['create', 'edit'])which excludes only create and edit, leaving index, store, show, update, and destroy. B correctly usesonly(['index', 'show', 'store']). C and D use invalid methodsskipandfilter.Final Answer:
Route::resource('products', ProductController::class)->only(['index', 'show', 'store']); -> Option BQuick Check:
Limit resource routes with only([...]) [OK]
- Using invalid methods like skip or filter
- Confusing only with except
- Not passing array to only method
Ready to prove it?
Pass the test and this pattern moves unknown
