Challenge - 5 Problems
API Resource Controller Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel API resource controller method?
Consider a Laravel API resource controller with this method:
What JSON response will be returned when calling
public function show($id) {
return response()->json(['id' => $id, 'name' => 'Item '.$id]);
}What JSON response will be returned when calling
show(5)?Laravel
public function show($id) {
return response()->json(['id' => $id, 'name' => 'Item '.$id]);
}Attempts:
2 left
💡 Hint
Look at how the string concatenation works with the $id variable.
✗ Incorrect
The method returns a JSON response with the id as an integer 5 and the name as 'Item 5'. Route parameters are strings by default but Laravel casts numeric route parameters to integers when passed to controller methods.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a Laravel API resource controller route?
You want to register a resource controller named
ProductController for API routes in routes/api.php. Which route definition is correct?Attempts:
2 left
💡 Hint
API resource routes exclude create/edit routes and require the controller class reference.
✗ Incorrect
The correct syntax for API resource routes uses Route::apiResource with the controller class reference using ::class.
❓ state_output
advanced2:00remaining
What is the HTTP status code returned by this API resource controller method?
Given this method in a Laravel API resource controller:
What HTTP status code will the client receive after a successful POST request?
public function store(Request $request) {
$data = $request->validate(['name' => 'required']);
$item = Item::create($data);
return response()->json($item, 201);
}What HTTP status code will the client receive after a successful POST request?
Laravel
public function store(Request $request) {
$data = $request->validate(['name' => 'required']);
$item = Item::create($data);
return response()->json($item, 201);
}Attempts:
2 left
💡 Hint
201 means resource created successfully.
✗ Incorrect
The method explicitly returns a JSON response with status code 201, which means 'Created'.
🔧 Debug
advanced2:00remaining
Why does this Laravel API resource controller method cause a runtime error?
Examine this method:
What causes the error when calling this method?
public function update(Request $request, $id) {
$item = Item::findOrFail($id);
$item->update($request->all);
return response()->json($item);
}What causes the error when calling this method?
Laravel
public function update(Request $request, $id) {
$item = Item::findOrFail($id);
$item->update($request->all);
return response()->json($item);
}Attempts:
2 left
💡 Hint
Check how methods are called on the Request object.
✗ Incorrect
The error is caused because $request->all is a method and must be called with parentheses: $request->all().
🧠 Conceptual
expert2:00remaining
Which statement best describes the difference between Route::resource and Route::apiResource in Laravel?
Choose the most accurate explanation about these two routing methods:
Attempts:
2 left
💡 Hint
Think about which routes are included or excluded for API-only controllers.
✗ Incorrect
Route::resource creates all RESTful routes including those for HTML views like create and edit, while Route::apiResource excludes these since APIs usually don't serve HTML forms.