0
0
Laravelframework~20 mins

Optional parameters in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 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.