Bird
0
0

What will be the output of this Laravel code snippet if the URL is '/products?filter=popular':

medium📝 component behavior Q4 of 15
Laravel - Request and Response
What will be the output of this Laravel code snippet if the URL is '/products?filter=popular':
Route::get('/products', function (Request $request) {
    return $request->query('filter', 'new');
});
Anew
Bpopular
Cnull
Derror
Step-by-Step Solution
Solution:
  1. Step 1: Check the URL query parameter

    The URL contains 'filter=popular' as a query parameter.
  2. Step 2: Understand the query() method behavior

    $request->query('filter', 'new') returns the value of 'filter' if present, else 'new'. Here, 'popular' is present.
  3. Final Answer:

    popular -> Option B
  4. Quick Check:

    Query param present returns value, else default [OK]
Quick Trick: Query param returns actual value if present, else default [OK]
Common Mistakes:
  • Assuming default is always returned
  • Confusing query() with input()
  • Expecting null when param exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes