0
0
Laravelframework~10 mins

API routes 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 GET API route that returns a welcome message.

Laravel
Route::[1]('/welcome', function () {
    return response()->json(['message' => 'Welcome to the API']);
});
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for fetching data.
Confusing HTTP methods.
2fill in blank
medium

Complete the code to define a POST API route that calls the UserController's store method.

Laravel
Route::[1]('/users', [UserController::class, 'store']);
Drag options to blanks, or click blank then click option'
Apost
Bget
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for creating data.
Mixing up controller method names.
3fill in blank
hard

Fix the error in the route definition to correctly define a route that updates a user by ID using PUT method.

Laravel
Route::[1]('/users/{id}', [UserController::class, 'update']);
Drag options to blanks, or click blank then click option'
Apost
Bget
Cdelete
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of PUT for updating data.
Forgetting to include the {id} parameter in the route.
4fill in blank
hard

Fill both blanks to define a route group with middleware 'auth:sanctum'.

Laravel
Route::[1](['middleware' => '[2]'], function () {
    // API routes here
});
Drag options to blanks, or click blank then click option'
Agroup
Bget
Cauth:sanctum
Dapi
Attempts:
3 left
💡 Hint
Common Mistakes
Using Route::get instead of Route::group for grouping.
Incorrect middleware name.
5fill in blank
hard

Fill both blanks to define a resource route for 'posts' using PostController with only 'index' and 'show' methods.

Laravel
Route::resource('posts', PostController::class)->only(['[1]', '[2]']);
Drag options to blanks, or click blank then click option'
Astore
Bindex
Cshow
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Including methods like 'store' or 'update' when only 'index' and 'show' are needed.
Misspelling method names.