Bird
Raised Fist0
Laravelframework~20 mins

Optional parameters in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Laravel Optional Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does Laravel handle optional route parameters?

Consider this Laravel route definition:

Route::get('/user/{id?}', function ($id = null) { return $id ?? 'guest'; });

What will be the output when you visit /user URL?

Laravel
Route::get('/user/{id?}', function ($id = null) { return $id ?? 'guest'; });
AIt returns null because the default value is null.
BIt throws a 404 error because the parameter is missing.
CIt returns 'guest' because the parameter is optional and not provided.
DIt returns an empty string because no parameter was passed.
Attempts:
2 left
💡 Hint

Think about what happens when an optional parameter is not given in the URL.

📝 Syntax
intermediate
2:00remaining
Which route definition correctly uses an optional parameter with a default value?

Choose the correct Laravel route definition that sets an optional parameter page with a default value of 1.

ARoute::get('/posts/{page?}', function ($page = 1) { return $page; });
BRoute::get('/posts/{page}', function ($page = 1) { return $page; });
CRoute::get('/posts/{page?}', function ($page) { return $page ?? 1; });
DRoute::get('/posts/{page?}', function ($page = null) { return $page; });
Attempts:
2 left
💡 Hint

Remember that optional parameters must have a question mark in the route and a default value in the function.

❓ state_output
advanced
2:00remaining
What is the output of this controller method with optional parameters?

Given this Laravel controller method:

public function show($category = 'all', $page = 1) {
    return "Category: $category, Page: $page";
}

If the route is defined as Route::get('/items/{category?}/{page?}', [ItemController::class, 'show']); and the URL /items/books is visited, what is the output?

Laravel
public function show($category = 'all', $page = 1) {
    return "Category: $category, Page: $page";
}
ACategory: all, Page: books
BCategory: books, Page: 1
CCategory: all, Page: 1
DCategory: books, Page: books
Attempts:
2 left
💡 Hint

Think about how Laravel matches optional parameters in order.

🔧 Debug
advanced
2:00remaining
Why does this Laravel route with optional parameters cause a 404 error?

Consider this route:

Route::get('/profile/{user?}/{section?}', function ($user = 'guest', $section = 'overview') {
    return "$user - $section";
});

Visiting /profile returns a 404 error. Why?

Laravel
Route::get('/profile/{user?}/{section?}', function ($user = 'guest', $section = 'overview') {
    return "$user - $section";
});
ABecause Laravel requires the first optional parameter if the second is optional, so both must be present or none.
BBecause the route parameters are not marked optional correctly with question marks.
CBecause the default values in the function are ignored by Laravel routing.
DBecause Laravel does not support multiple optional parameters in routes.
Attempts:
2 left
💡 Hint

Think about how Laravel matches optional parameters in sequence.

🧠 Conceptual
expert
2:00remaining
How does Laravel prioritize route matching with optional parameters?

Given these two routes:

Route::get('/product/{id}', function ($id) { return "Product $id"; });
Route::get('/product/{id?}', function ($id = 'default') { return "Optional Product $id"; });

What happens when you visit /product and why?

AIt matches the first route and returns 'Product' with an empty id.
BIt matches the first route and throws an error because id is missing.
CIt causes a routing conflict error because both routes match the same URL.
DIt matches the second route and returns 'Optional Product default' because the first route requires an id.
Attempts:
2 left
💡 Hint

Think about how Laravel matches routes in order and optional parameters.

Practice

(1/5)
1. What is the main purpose of optional parameters in Laravel functions or routes?
easy
A. To allow functions or routes to work with or without extra information
B. To force users to always provide all parameters
C. To make parameters required by default
D. To disable parameter validation

Solution

  1. Step 1: Understand optional parameters

    Optional parameters let functions or routes accept calls even if some parameters are missing.
  2. Step 2: Identify the purpose

    This flexibility means the function or route can work with or without extra info, improving usability.
  3. Final Answer:

    To allow functions or routes to work with or without extra information -> Option A
  4. Quick Check:

    Optional parameters = flexible input [OK]
Hint: Optional means parameters can be skipped safely [OK]
Common Mistakes:
  • Thinking optional means parameters are required
  • Confusing optional with disabling validation
  • Assuming optional parameters come before required ones
2. Which of the following is the correct way to define an optional parameter in a Laravel route closure?
easy
A. Route::get('/user/{id?}', function ($id) { return $id; });
B. Route::get('/user/{id?}', function ($id = null) { return $id; });
C. Route::get('/user/{?id}', function ($id) { return $id; });
D. Route::get('/user/{id}', function ($id = null) { return $id; });

Solution

  1. Step 1: Check route parameter syntax

    Optional parameters in routes use a question mark after the parameter name: {id?}.
  2. Step 2: Check function parameter default

    The function parameter must have a default value, like $id = null, to be optional.
  3. Final Answer:

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

    Optional param syntax = {id?} + default value [OK]
Hint: Use {param?} and default value in function [OK]
Common Mistakes:
  • Omitting the question mark in route parameter
  • Not giving a default value in function parameter
  • Placing question mark inside braces incorrectly
3. Given this Laravel controller method:
public function show($id, $page = 1) {
    return "ID: $id, Page: $page";
}

What will be the output of show(5)?
medium
A. "ID: 5, Page: "
B. "ID: , Page: 1"
C. Error: Missing argument for $page
D. "ID: 5, Page: 1"

Solution

  1. Step 1: Identify parameters and defaults

    The method has a required parameter $id and an optional $page with default 1.
  2. Step 2: Call with one argument

    Calling show(5) sets $id=5 and uses default $page=1.
  3. Final Answer:

    "ID: 5, Page: 1" -> Option D
  4. Quick Check:

    Optional param uses default when missing [OK]
Hint: Optional params use default if not passed [OK]
Common Mistakes:
  • Expecting error when optional param is missing
  • Assuming optional param is empty string
  • Confusing order of parameters
4. Consider this Laravel route definition:
Route::get('/post/{id?}/{comment?}', function ($id = 1, $comment = 'none') {
    return "$id - $comment";
});

What is the error in this code?
medium
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

  1. Step 1: Check order of optional parameters

    In Laravel, optional parameters must come after all required ones and be at the end in order.
  2. 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.
  3. Step 3: Identify route matching issue

    Laravel cannot distinguish which parameter is missing if earlier optional parameters are skipped but later ones are present.
  4. Final Answer:

    Optional parameters must be at the end; $id cannot be optional before $comment -> Option C
  5. 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

  1. 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.
  2. Step 2: Match controller method parameters

    The method must have $category first (required) and $page second with default 1.
  3. 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.
  4. Final Answer:

    Option A with required category and optional page correctly defined -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard