Bird
0
0

Identify the error in this Laravel code snippet that tries to get a query parameter sort:

medium📝 Debug Q14 of 15
Laravel - Request and Response
Identify the error in this Laravel code snippet that tries to get a query parameter sort:
Route::get('/items', function (Request $request) {
    $sort = $request->query['sort'];
    return $sort;
});
ARequest class not imported
BUsing array syntax instead of method call for query parameters
CIncorrect route method; should be post() instead of get()
DMissing semicolon after return statement
Step-by-Step Solution
Solution:
  1. Step 1: Check how query parameters are accessed

    The code uses $request->query['sort'], which is incorrect because query is a method, not an array.
  2. Step 2: Correct syntax for query parameters

    The correct way is $request->query('sort') to get the 'sort' parameter.
  3. Final Answer:

    Using array syntax instead of method call for query parameters -> Option B
  4. Quick Check:

    Use $request->query('key'), not $request->query['key'] [OK]
Quick Trick: query() is a method, not an array; use parentheses [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Confusing get() with query() method
  • Assuming query is a property, not a method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes