0
0
Laravelframework~10 mins

Dependency injection in controllers 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 inject the service into the controller constructor.

Laravel
public function __construct([1] $service) {
    $this->service = $service;
}
Drag options to blanks, or click blank then click option'
AController
BRequest
CUserService
DResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using framework classes like Request or Response instead of the service class.
Forgetting to type hint the service class in the constructor.
2fill in blank
medium

Complete the method signature to inject the Request object.

Laravel
public function store([1] $request) {
    // method body
}
Drag options to blanks, or click blank then click option'
AResponse
BRequest
CUserService
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Injecting the service class instead of the Request.
Using Response or Controller as the parameter type.
3fill in blank
hard

Fix the error in the controller method to correctly inject the service.

Laravel
public function show([1] $userService, $id) {
    return $userService->find($id);
}
Drag options to blanks, or click blank then click option'
AUserService
BController
CResponse
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request or Response instead of the service class.
Not type hinting the service class.
4fill in blank
hard

Fill both blanks to inject the service and call its method inside the controller.

Laravel
public function update([1] $service, $id) {
    return $service->[2]($id);
}
Drag options to blanks, or click blank then click option'
AUserService
Bdelete
Cupdate
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong method name like delete instead of update.
Injecting Request instead of the service.
5fill in blank
hard

Fill all three blanks to inject the service, the request, and call the correct method.

Laravel
public function store([1] $service, [2] $request) {
    return $service->[3]($request->all());
}
Drag options to blanks, or click blank then click option'
AUserService
BRequest
Ccreate
DResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of parameters.
Using Response instead of Request.
Calling the wrong method like update instead of create.