Bird
Raised Fist0
Laravelframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a controller method (action) in Laravel?
easy
A. To handle a specific user request and return a response
B. To define database schema
C. To style the HTML output
D. To manage server configuration

Solution

  1. Step 1: Understand controller role

    Controllers group related logic into methods called actions.
  2. Step 2: Identify action purpose

    Each action handles a user request and returns a response like a view or JSON.
  3. Final Answer:

    To handle a specific user request and return a response -> Option A
  4. Quick Check:

    Controller action = handle request and respond [OK]
Hint: Controllers handle requests and responses, not styling or config [OK]
Common Mistakes:
  • Confusing controller methods with database or styling tasks
  • Thinking controllers manage server settings
  • Assuming controllers only return views
2. Which of the following is the correct syntax to define a controller method named show in Laravel?
easy
A. function show() public { /* code */ }
B. public function show() { /* code */ }
C. public show function() { /* code */ }
D. def show() { /* code */ }

Solution

  1. Step 1: Recall PHP method syntax

    In PHP, methods are defined with visibility, function keyword, name, and parentheses.
  2. Step 2: Match Laravel controller method syntax

    Laravel uses PHP syntax: public function methodName() {}.
  3. Final Answer:

    public function show() { /* code */ } -> Option B
  4. Quick Check:

    PHP method = public function name() [OK]
Hint: Remember PHP method syntax: visibility + function + name() [OK]
Common Mistakes:
  • Placing visibility after function keyword
  • Using Python style def keyword
  • Omitting parentheses after method name
3. Given this controller method in Laravel:
public function greet($name) {
    return "Hello, " . $name . "!";
}

What will be the output if called with $controller->greet('Anna')?
medium
A. "Hello, Anna!"
B. "Hello, $name!"
C. Error: Undefined variable
D. "Hello, !"

Solution

  1. Step 1: Understand method parameter usage

    The method receives $name and concatenates it with strings.
  2. Step 2: Substitute argument value

    Calling with 'Anna' replaces $name with 'Anna', so output is "Hello, Anna!".
  3. Final Answer:

    "Hello, Anna!" -> Option A
  4. Quick Check:

    Parameter value used in string = "Hello, Anna!" [OK]
Hint: Replace parameter with argument value to find output [OK]
Common Mistakes:
  • Treating $name as literal string
  • Expecting error due to missing variable
  • Ignoring string concatenation
4. Identify the error in this Laravel controller method:
public function index()
{
    return view('welcome')
}
medium
A. View name should be capitalized
B. Incorrect method name
C. Missing semicolon after return statement
D. Missing parentheses in view() call

Solution

  1. Step 1: Check PHP syntax in method

    PHP statements must end with a semicolon; here it is missing after return view('welcome').
  2. Step 2: Validate other parts

    Method name and view call syntax are correct; view name case is not an error.
  3. Final Answer:

    Missing semicolon after return statement -> Option C
  4. Quick Check:

    PHP statements end with ; [OK]
Hint: Check for missing semicolons after return statements [OK]
Common Mistakes:
  • Ignoring missing semicolon causing syntax error
  • Thinking view names must be capitalized
  • Assuming parentheses are missing in view()
5. You want to create a Laravel controller method that returns a JSON response with a user's name and email. Which method correctly does this?
public function userInfo()
{
    $user = ['name' => 'John', 'email' => 'john@example.com'];
    // Fill in the return statement
}
hard
A. return json($user);
B. return json_response($user);
C. return response->json($user);
D. return response()->json($user);

Solution

  1. Step 1: Recall Laravel JSON response syntax

    Laravel uses response()->json() helper to return JSON responses.
  2. Step 2: Check each option's syntax

    return response()->json($user); correctly calls response()->json($user);. Others are invalid or incorrect syntax.
  3. Final Answer:

    return response()->json($user); -> Option D
  4. Quick Check:

    Use response()->json() for JSON output [OK]
Hint: Use response()->json() helper to return JSON [OK]
Common Mistakes:
  • Using undefined functions like json() or json_response()
  • Missing parentheses or arrow operator in response call
  • Confusing PHP syntax for method calls
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard