A. Function parameters must not have default values
B. Default values cannot be strings
C. Optional parameters must be at the end; $id cannot be optional before $comment
D. Route parameters cannot have question marks
Solution
Step 1: Check order of optional parameters
In Laravel, optional parameters must come after all required ones and be at the end in order.
Step 2: Analyze parameter order
Here, $id is optional before $comment which is also optional, but $id is first. This is allowed only if both are optional and route matches accordingly.
Step 3: Identify route matching issue
Laravel cannot distinguish which parameter is missing if earlier optional parameters are skipped but later ones are present.
Final Answer:
Optional parameters must be at the end; $id cannot be optional before $comment -> Option C
Quick Check:
Optional params order matters [OK]
Hint: Put optional params only at the end [OK]
Common Mistakes:
Ignoring order of optional parameters
Thinking default values cannot be strings
Believing question marks are invalid in routes
5. You want to create a Laravel route that accepts a required category and an optional page number with default 1. Which of these route and controller method pairs is correct?
hard
A. Route::get('/items/{category}/{page?}', [ItemController::class, 'index']); public function index($category, $page = 1) { return "$category - $page"; }
B. Route::get('/items/{category?}/{page}', [ItemController::class, 'index']); public function index($category = 'all', $page) { return "$category - $page"; }
C. Route::get('/items/{category}/{page}', [ItemController::class, 'index']); public function index($category, $page = 1) { return "$category - $page"; }
D. Route::get('/items/{category?}/{page?}', [ItemController::class, 'index']); public function index($category = 'all', $page = 1) { return "$category - $page"; }
Solution
Step 1: Identify required and optional parameters in route
The required parameter category must come first without a question mark. The optional page comes after with a question mark.
Step 2: Match controller method parameters
The method must have $category first (required) and $page second with default 1.
Step 3: Check each option
Route::get('/items/{category}/{page?}', [ItemController::class, 'index']); public function index($category, $page = 1) { return "$category - $page"; } matches the correct route and method signature. Others either reverse order or make required params optional incorrectly.
Final Answer:
Option A with required category and optional page correctly defined -> Option A
Quick Check:
Required first, optional after with defaults [OK]
Hint: Required params first, optional with default after [OK]
Common Mistakes:
Making required params optional in route
Placing optional params before required ones
Not giving default values in method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.