0
0
Laravelframework~10 mins

Optional parameters in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a route with an optional parameter named 'id'.

Laravel
Route::get('/user/{id[1]', function ($id = null) {
    return $id ? "User ID: $id" : 'No ID provided';
});
Drag options to blanks, or click blank then click option'
A?
B+
C*
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using symbols like ! or * which are not valid for optional parameters.
Forgetting to provide a default value in the function parameter.
2fill in blank
medium

Complete the controller method signature to make the parameter 'page' optional with a default value of 1.

Laravel
public function showPage([1] $page = 1) {
    return "Showing page: $page";
}
Drag options to blanks, or click blank then click option'
Aarray
Bstring
Cbool
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string type which may cause type errors.
Not specifying a type hint at all.
3fill in blank
hard

Fix the error in the route definition by correctly marking the 'category' parameter as optional.

Laravel
Route::get('/products/{category[1]', function ($category = 'all') {
    return "Category: $category";
});
Drag options to blanks, or click blank then click option'
A?
B!
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using symbols other than question mark for optional parameters.
Not providing a default value in the function.
4fill in blank
hard

Fill both blanks to define a route with an optional 'slug' parameter and a default value in the closure.

Laravel
Route::get('/post/{slug[1]', function ($slug [2] 'default-post') {
    return "Post slug: $slug";
});
Drag options to blanks, or click blank then click option'
A?
B=
C!
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!' or ':' instead of '?' for optional parameters.
Not assigning a default value in the closure parameter.
5fill in blank
hard

Fill all three blanks to create a controller method with an optional string parameter 'name' defaulting to 'Guest' and return a greeting.

Laravel
public function greet([1] $name [2] 'Guest') {
    return "Hello, [3]!";
}
Drag options to blanks, or click blank then click option'
Astring
B=
C$name
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong type hint like int.
Forgetting the default value assignment.
Not using the parameter variable in the return.