Bird
0
0

Which of the following is the correct way to define an optional parameter in a Laravel route?

easy📝 Conceptual Q2 of 15
Laravel - Routing
Which of the following is the correct way to define an optional parameter in a Laravel route?
ARoute::get('/user/{id?}', function ($id = null) { ... });
BRoute::get('/user/{id}', function ($id = null) { ... });
CRoute::get('/user/{id?}', function ($id) { ... });
DRoute::get('/user/{id}', function ($id) { ... });
Step-by-Step Solution
Solution:
  1. Step 1: Identify optional parameter syntax in routes

    Optional parameters in routes are marked with a question mark after the parameter name.
  2. Step 2: Match with controller parameter default value

    The corresponding function parameter must have a default value, usually null, to be optional.
  3. Final Answer:

    Route::get('/user/{id?}', function ($id = null) { ... }); -> Option A
  4. Quick Check:

    Route param with ? + default value = Correct [OK]
Quick Trick: Use ? in route and default value in function [OK]
Common Mistakes:
  • Omitting the ? in the route parameter
  • Not providing a default value in the function
  • Marking parameter optional in route but not in function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes