0
0
Laravelframework~10 mins

Why controllers organize request handling in Laravel - Test Your Understanding

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 handles a request.

Laravel
public function index() {
    return view([1]);
}
Drag options to blanks, or click blank then click option'
A'home.index'
Bhome.index
C"home.index"
Dhome_index
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the view name
Using dot notation without quotes
2fill in blank
medium

Complete the code to import the base Controller class in Laravel.

Laravel
use [1];
Drag options to blanks, or click blank then click option'
AApp\Http\Controllers\Controller
BIlluminate\Http\Controller
CApp\Controller
DHttp\Controllers\Controller
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong namespace for Controller
Missing the full namespace path
3fill in blank
hard

Fix the error in the controller method to correctly retrieve all users.

Laravel
public function showUsers() {
    $users = User::[1]();
    return view('users.list', ['users' => $users]);
}
Drag options to blanks, or click blank then click option'
AgetAll
Bretrieve
Call
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like getAll or fetch
Forgetting parentheses after method name
4fill in blank
hard

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

Laravel
public function store(Request $request) {
    $validated = $request->[1]([ 'title' => 'required', 'body' => 'required' ]);
    Post::[2]($validated);
    return redirect()->route('posts.index');
}
Drag options to blanks, or click blank then click option'
Avalidate
Bsave
Ccreate
Dstore
Attempts:
3 left
💡 Hint
Common Mistakes
Using save() on the model class instead of create()
Not validating request data before saving
5fill in blank
hard

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

Laravel
public function update(Request $request, $id) {
    $user = User::findOrFail([1]);
    $user->update($request->[2]());
    return redirect()->route('users.show', [3])->with('success', 'User updated!');
}
Drag options to blanks, or click blank then click option'
A$id
Ball
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for user id
Using input() instead of all() for update data
Not passing id to redirect route