Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to retrieve all records from the User model.
Laravel
$users = User::[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using find() which requires an ID parameter.
Using select() which needs additional query building.
✗ Incorrect
The all() method retrieves all records from the database table associated with the model.
2fill in blank
mediumComplete the code to find a User by its primary key.
Laravel
$user = User::[1]($id); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using all() which returns all records.
Using where() which returns a query builder, not a model instance.
✗ Incorrect
The find() method retrieves a record by its primary key.
3fill in blank
hardFix the error in the code to update a User's name and save it.
Laravel
$user = User::find($id);
$user->name = [1];
$user->save(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string literals.
Using bare words without quotes causing errors.
✗ Incorrect
String values must be enclosed in quotes. Double quotes "John" or single quotes 'John' are valid.
4fill in blank
hardFill both blanks to create a new User and save it to the database.
Laravel
$user = new User(); $user->[1] = 'alice@example.com'; $user->[2]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using create() on an instance instead of save().
Trying to update() a new model before saving.
✗ Incorrect
Set the email attribute, then call save() to store the new user.
5fill in blank
hardFill all three blanks to delete a User by ID safely.
Laravel
$user = User::[1]($id); if ($user) { $user->[2](); } else { [3]('User not found'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using destroy() on an instance instead of find().
Not checking if the user exists before deleting.
Not handling the case when user is not found.
✗ Incorrect
Use find() to get the user, delete() to remove it, and abort() to handle missing user gracefully.