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?
Route::get('/search', function (Request $request) { return $request->query('q'); });
Think about how Laravel's query() method extracts values from the URL.
The query('q') method returns the value of the query parameter named 'q'. Since the URL has ?q=books, it returns "books".
Given a URL /filter?category=books&sort=asc, which code snippet correctly retrieves both parameters?
Remember that query() is the recommended method to get query parameters.
Option B uses query() to get each parameter individually, which is the correct and clear way. Option B uses input() which also works but is less specific. Option B uses get(), which also works but is less specific. Option B tries to get all parameters but then accesses them as an array which is valid but less direct.
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;
}Check if the Request class is properly imported and used.
If the Request class is not imported or the parameter is not type-hinted as Illuminate\Http\Request, Laravel won't inject the request object properly, so $request->query('page') returns null.
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?
Recall what $request->query() returns when called without arguments.
The query() method without arguments returns all query parameters as an associative array. Encoding it to JSON produces a JSON object with keys and values matching the URL parameters.
Choose the correct statement about how Laravel handles query parameters in HTTP requests.
Think about how Laravel combines input sources.
Laravel's $request->input() method returns data from all input sources including query parameters and POST data. The query() method returns only query parameters. This merging happens regardless of the HTTP method.