B. Using array syntax instead of method call for query parameters
C. Incorrect route method; should be post() instead of get()
D. Missing semicolon after return statement
Solution
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.
Step 2: Correct syntax for query parameters
The correct way is $request->query('sort') to get the 'sort' parameter.
Final Answer:
Using array syntax instead of method call for query parameters -> Option B
Quick Check:
Use $request->query('key'), not $request->query['key'] [OK]
Hint: 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
5. You want to get a query parameter filter in Laravel but provide a default value of all if it is missing or empty. Which code correctly handles this?
hard
A. $filter = $request->query('filter') ?: 'all';
B. $filter = $request->input('filter', 'all');
C. $filter = $request->query('filter', 'all');
D. $filter = $request->query('filter') ?? 'all';
Solution
Step 1: Understand default value behavior in query()
The second argument in query('filter', 'all') returns 'all' only if the parameter is missing, but not if empty.
Step 2: Handle empty values as well
Using $request->query('filter') ?: 'all' returns 'all' if the parameter is missing or empty (falsy).
Step 3: Check other options
$filter = $request->input('filter', 'all'); uses input() which includes POST data, not just query. $filter = $request->query('filter') ?? 'all'; uses null coalescing which does not treat empty string as missing.
Final Answer:
$filter = $request->query('filter') ?: 'all'; -> Option A
Quick Check:
Use ?: to handle missing or empty query params = C [OK]
Hint: Use ?: to cover missing or empty query parameters [OK]
Common Mistakes:
Assuming default in query() covers empty strings
Using input() which mixes POST and query data
Using ?? which ignores empty strings
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.