0
0
Laravelframework~20 mins

Controller methods and actions in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Controller Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel controller method?
Consider this controller method in Laravel:
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']);
}
A{"id":"5","status":"found"}
B{"id":5,"status":"found"}
C{"id":5,"status":"not found"}
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that the $id parameter is passed as a string from the route and quoted in the JSON.
lifecycle
intermediate
1: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?
Astore()
Bcreate()
Cedit()
Dupdate()
Attempts:
2 left
💡 Hint
Think about which method processes the form submission to save data.
📝 Syntax
advanced
2:30remaining
What error does this Laravel controller method produce?
Examine this controller method:
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');
}
ASyntaxError: Unexpected token
BNo error, updates successfully
CError: Method all() not found
DTypeError: Argument 1 passed to update() must be array, null given
Attempts:
2 left
💡 Hint
Check how the request data is accessed.
🔧 Debug
advanced
2:30remaining
Why does this Laravel controller method cause a runtime error?
Look at this method:
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();
}
ANo error, deletes successfully
BModelNotFoundException if id 10 does not exist
CError: Call to undefined method delete (missing parentheses)
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check how the delete method is called.
🧠 Conceptual
expert
2:00remaining
What is the value of $result after this Laravel controller action?
Given this controller method:
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'));
}
A7
B0
Cnull
DError: count() method not found
Attempts:
2 left
💡 Hint
The get() method returns a collection of matching items.