0
0
Laravelframework~20 mins

Query parameters in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Parameters Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Laravel handle query parameters in routes?

Consider a Laravel route defined as Route::get('/search', function (Request $request) { return $request->query('q'); });. What will be the output if the URL is /search?q=books?

Laravel
Route::get('/search', function (Request $request) { return $request->query('q'); });
A"books"
B"q=books"
Cnull
DAn error is thrown
Attempts:
2 left
💡 Hint

Think about how Laravel's query() method extracts values from the URL.

📝 Syntax
intermediate
2:00remaining
Which code correctly retrieves multiple query parameters in Laravel?

Given a URL /filter?category=books&sort=asc, which code snippet correctly retrieves both parameters?

A$params = $request->input(['category', 'sort']);
B$category = $request->query('category'); $sort = $request->query('sort');
C$category = $request->get('category'); $sort = $request->get('sort');
D$params = $request->query(); $category = $params['category']; $sort = $params['sort'];
Attempts:
2 left
💡 Hint

Remember that query() is the recommended method to get query parameters.

🔧 Debug
advanced
2:00remaining
Why does this Laravel code fail to get the query parameter?

Given the route and controller method below, why does $request->query('page') return null when accessing /items?page=2?

Route::get('/items', [ItemController::class, 'index']);

public function index(Request $request) {
    $page = $request->query('page');
    return $page;
}
AThe route is missing a name, so query parameters are ignored.
BThe query method only works for POST requests, not GET.
CThe Request object is not imported or type-hinted correctly.
DThe query parameter 'page' is not passed because the URL is malformed.
Attempts:
2 left
💡 Hint

Check if the Request class is properly imported and used.

state_output
advanced
2:00remaining
What is the output of this Laravel controller method?

Consider this controller method:

public function show(Request $request) {
    $filters = $request->query();
    return json_encode($filters);
}

What is the output when accessing /show?color=red&size=medium?

AA JSON array: ["color", "red", "size", "medium"]
BAn empty JSON object: "{}"
CA syntax error occurs
D{"color":"red","size":"medium"}
Attempts:
2 left
💡 Hint

Recall what $request->query() returns when called without arguments.

🧠 Conceptual
expert
2:00remaining
Which statement about Laravel query parameters is true?

Choose the correct statement about how Laravel handles query parameters in HTTP requests.

ALaravel automatically merges query parameters and POST data, so <code>$request->input()</code> returns both.
BQuery parameters can only be accessed via <code>$request->input()</code> and not <code>$request->query()</code>.
CThe <code>$request->query()</code> method only returns query parameters if the route uses a GET method.
DQuery parameters are not accessible in controller methods unless explicitly passed as route parameters.
Attempts:
2 left
💡 Hint

Think about how Laravel combines input sources.