0
0
Laravelframework~10 mins

Controller methods and actions 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 controller method that returns a view named 'welcome'.

Laravel
public function index() {
    return view('[1]');
}
Drag options to blanks, or click blank then click option'
Awelcome
Bindex
Cdashboard
Dhome
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong view name causes a 'view not found' error.
Forgetting to return the view from the method.
2fill in blank
medium

Complete the code to define a controller method that accepts a parameter $id and returns it.

Laravel
public function show([1]) {
    return $id;
}
Drag options to blanks, or click blank then click option'
A$id
B$name
C$user
D$data
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the variable returned.
Forgetting the $ sign before the parameter name.
3fill in blank
hard

Fix the error in the controller method to correctly redirect to the 'home' route.

Laravel
public function redirectToHome() {
    return [1]('home');
}
Drag options to blanks, or click blank then click option'
Aview
BredirectTo
Credirect
Droute
Attempts:
3 left
💡 Hint
Common Mistakes
Using view instead of redirect causes the method to return a view, not a redirect.
Using route alone without redirect does not perform a redirect.
4fill in blank
hard

Fill both blanks to define a controller method that validates a request and stores a new user.

Laravel
public function store(Request $request) {
    $validated = $request->[1]([ 'email' => 'required|email' ]);
    User::[2]($validated);
    return redirect()->route('users.index');
}
Drag options to blanks, or click blank then click option'
Avalidate
Bsave
Ccreate
Dcheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using save on the model class instead of an instance.
Using check which is not a Laravel method.
5fill in blank
hard

Fill all three blanks to define a controller method that updates a user and redirects back with a success message.

Laravel
public function update(Request $request, User $user) {
    $data = $request->[1]([ 'name' => 'required' ]);
    $user->[2]($data);
    return redirect()->[3]('users.show', $user->id)->with('status', 'User updated!');
}
Drag options to blanks, or click blank then click option'
Avalidate
Bupdate
Croute
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using save instead of update on the model instance.
Using redirect()->route incorrectly without parameters.