Challenge - 5 Problems
Laravel Controller Mastery
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 controller method?
Consider this controller method in Laravel:
What will be the JSON response when calling
public function show($id) {
return response()->json(['id' => $id, 'status' => 'found']);
}What will be the JSON response when calling
show(5)?Laravel
public function show($id) {
return response()->json(['id' => $id, 'status' => 'found']);
}Attempts:
2 left
💡 Hint
Remember that the
$id parameter is passed as a string from the route and quoted in the JSON.✗ Incorrect
The method returns a JSON response with the
id key set to the integer passed and status set to 'found'. Option B matches this exactly.❓ lifecycle
intermediate1:30remaining
Which controller method is called when a resource is created in Laravel?
In a Laravel resource controller, which method handles the HTTP POST request to create a new resource?
Attempts:
2 left
💡 Hint
Think about which method processes the form submission to save data.
✗ Incorrect
The
store() method handles POST requests to save new resources. create() shows the form, but does not save.📝 Syntax
advanced2:30remaining
What error does this Laravel controller method produce?
Examine this controller method:
What error will occur when this method runs?
public function update(Request $request, $id) {
$data = $request->all;
Model::find($id)->update($data);
return redirect()->route('items.index');
}What error will occur when this method runs?
Laravel
public function update(Request $request, $id) {
$data = $request->all;
Model::find($id)->update($data);
return redirect()->route('items.index');
}Attempts:
2 left
💡 Hint
Check how the request data is accessed.
✗ Incorrect
The code uses
$request->all without parentheses, so it is treated as a property, which is null. The update method expects an array, causing a TypeError.🔧 Debug
advanced2:30remaining
Why does this Laravel controller method cause a runtime error?
Look at this method:
What causes the error when calling
public function destroy($id) {
$item = Model::findOrFail($id);
$item->delete;
return redirect()->back();
}What causes the error when calling
destroy(10)?Laravel
public function destroy($id) {
$item = Model::findOrFail($id);
$item->delete;
return redirect()->back();
}Attempts:
2 left
💡 Hint
Check how the delete method is called.
✗ Incorrect
The code uses
$item->delete without parentheses, so it references the method but does not call it. This causes a runtime error.🧠 Conceptual
expert2:00remaining
What is the value of $result after this Laravel controller action?
Given this controller method:
What is the value of
public function index() {
$items = Model::where('active', true)->get();
$result = $items->count();
return view('items.index', compact('items'));
}What is the value of
$result if there are 7 active items in the database?Laravel
public function index() {
$items = Model::where('active', true)->get();
$result = $items->count();
return view('items.index', compact('items'));
}Attempts:
2 left
💡 Hint
The
get() method returns a collection of matching items.✗ Incorrect
The
count() method on the collection returns the number of items, which is 7 in this case.