Laravel - RoutingWhich 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) { ... });Check Answer
Step-by-Step SolutionSolution:Step 1: Identify optional parameter syntax in routesOptional parameters in routes are marked with a question mark after the parameter name.Step 2: Match with controller parameter default valueThe corresponding function parameter must have a default value, usually null, to be optional.Final Answer:Route::get('/user/{id?}', function ($id = null) { ... }); -> Option AQuick 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 parameterNot providing a default value in the functionMarking parameter optional in route but not in function
Master "Routing" in Laravel9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Laravel Quizzes Configuration and Environment - Debug mode - Quiz 2easy Configuration and Environment - Logging configuration - Quiz 7medium Controllers - Single action controllers - Quiz 6medium Laravel Basics and Architecture - Artisan CLI overview - Quiz 8hard Request and Response - Session basics - Quiz 8hard Request and Response - Accessing request data - Quiz 5medium Routing - Route naming - Quiz 7medium Routing - Route naming - Quiz 9hard Routing - Route groups - Quiz 2easy Views and Blade Templates - Control structures (@if, @foreach, @for) - Quiz 9hard