Performance: Query parameters
Query parameters affect server response time and client-side rendering speed by influencing database queries and URL parsing.
Route::get('/products', function (Request $request) { $query = DB::table('products'); if ($category = $request->query('category')) { $query->where('category', $category); } if ($minPrice = $request->query('min_price')) { $query->where('price', '>=', $minPrice); } if ($maxPrice = $request->query('max_price')) { $query->where('price', '<=', $maxPrice); } $products = $query->get(); return view('products.index', ['products' => $products]); });
Route::get('/products', function (Request $request) { $products = DB::table('products') ->where('category', $request->query('category')) ->orWhere('price', '>', $request->query('min_price')) ->orWhere('price', '<', $request->query('max_price')) ->get(); return view('products.index', ['products' => $products]); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unfiltered query parameters causing broad DB queries | N/A | N/A | Blocks rendering due to slow server response | [X] Bad |
| Filtered and validated query parameters with indexed DB columns | N/A | N/A | Fast server response enables quick paint | [OK] Good |
page from a request?$request->query('key') to get query parameters from the URL.$request->query('page') correctly accesses the query parameter named 'page'.Route::get('/search', function (Request $request) {
$term = $request->query('term', 'default');
return $term;
});/search?term=laravel?$request->query('term', 'default'), which returns the 'term' parameter if present, else 'default'.?term=laravel, so 'term' is 'laravel', not missing.sort:Route::get('/items', function (Request $request) {
$sort = $request->query['sort'];
return $sort;
});$request->query['sort'], which is incorrect because query is a method, not an array.$request->query('sort') to get the 'sort' parameter.filter in Laravel but provide a default value of all if it is missing or empty. Which code correctly handles this?query('filter', 'all') returns 'all' only if the parameter is missing, but not if empty.$request->query('filter') ?: 'all' returns 'all' if the parameter is missing or empty (falsy).Pass the test and this pattern moves unknown