📖 Scenario: You are building a simple Laravel web application that shows a list of products. You want to allow users to filter products by category using query parameters in the URL.
🎯 Goal: Build a Laravel route and controller method that reads a query parameter called category from the URL and returns only products matching that category.
📋 What You'll Learn
Create an array of products with exact keys and values
Add a variable to hold the query parameter value
Filter the products array using the query parameter
Return the filtered products in a Laravel controller method
💡 Why This Matters
🌍 Real World
Filtering data based on user input in URLs is common in web apps, such as showing products by category or articles by tag.
💼 Career
Understanding query parameters and filtering data is essential for backend developers working with Laravel or any web framework.
Progress0 / 4 steps
1
Create the products array
Create a PHP array called $products with these exact entries: ['id' => 1, 'name' => 'Laptop', 'category' => 'electronics'], ['id' => 2, 'name' => 'Chair', 'category' => 'furniture'], and ['id' => 3, 'name' => 'Smartphone', 'category' => 'electronics'].
Laravel
Hint
Use a PHP array with three associative arrays inside, each representing a product.
2
Get the category query parameter
Create a variable called $category that gets the category query parameter from the Laravel request using request()->query('category').
Laravel
Hint
Use Laravel's request()->query('category') to get the query parameter.
3
Filter products by category
Create a variable called $filteredProducts that filters $products to include only products where 'category' equals $category. Use array_filter with a callback function that checks $product['category'] === $category. If $category is empty, set $filteredProducts to $products without filtering.
Laravel
Hint
Use array_filter with a callback that compares the product's category to $category. Use an if statement to check if $category is set.
4
Return filtered products in controller method
Create a Laravel controller method called showProducts that returns the $filteredProducts array as a JSON response using return response()->json($filteredProducts);. Include all previous code inside this method.
Laravel
Hint
Wrap all code inside a controller method named showProducts. Use the Request object to get the query parameter and return JSON response.
Practice
(1/5)
1. What is the purpose of query parameters in a Laravel application URL?
easy
A. To define the database schema
B. To style the webpage with CSS
C. To send extra information to control the data returned by the server
D. To store user session data on the server
Solution
Step 1: Understand what query parameters do
Query parameters are parts of the URL that send extra info to the server, usually after a question mark.
Step 2: Identify their use in Laravel
In Laravel, query parameters help control what data the server returns based on the extra info sent.
Final Answer:
To send extra information to control the data returned by the server -> Option C
Quick Check:
Query parameters control returned data = B [OK]
Hint: Query parameters add info after ? in URL to filter data [OK]
Common Mistakes:
Confusing query parameters with database schema
Thinking query parameters style the page
Assuming query parameters store session data
2. Which is the correct Laravel syntax to get a query parameter named page from a request?
easy
A. $request->param('page')
B. $request->input('page')
C. $request->getQuery('page')
D. $request->query('page')
Solution
Step 1: Recall Laravel method for query parameters
Laravel uses $request->query('key') to get query parameters from the URL.
Step 2: Match the correct syntax
Among options, only $request->query('page') correctly accesses the query parameter named 'page'.
Final Answer:
$request->query('page') -> Option D
Quick Check:
Use $request->query('key') for query params = A [OK]
Hint: Use $request->query('key') to get query params in Laravel [OK]
Common Mistakes:
Using $request->input() which also works but includes POST data
Using non-existent methods like getQuery or param
Confusing query() with input() for query parameters
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.