Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to connect to the default database in Laravel.
Laravel
DB::[1]('select * from users');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' instead of 'select' to get data.
Trying to use 'update' or 'delete' when just reading data.
✗ Incorrect
The select method runs a query to get data from the database.
2fill in blank
mediumComplete the code to create a new user record using Eloquent.
Laravel
$user = new User(); $user->name = 'John'; $user->email = 'john@example.com'; $user->[1]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delete' instead of 'save' to add data.
Using 'find' which only retrieves data.
✗ Incorrect
The save method stores the new user record in the database.
3fill in blank
hardFix the error in the query builder to get users older than 18.
Laravel
$users = DB::table('users')->where('age', '[1]', 18)->get();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which selects only age exactly 18.
Using '<' which selects younger users.
✗ Incorrect
The operator '>' selects users older than 18.
4fill in blank
hardFill both blanks to update the email of a user with id 5.
Laravel
DB::table('users')->where('id', [1])->[2](['email' => 'new@example.com']);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delete' instead of 'update' to change data.
Using wrong id value.
✗ Incorrect
We specify the user id as 5 and use update to change the email.
5fill in blank
hardFill all three blanks to create a dictionary of user names and emails for users older than 20.
Laravel
$users = DB::table('users')->where('age', '[1]', 20)->pluck('[2]', '[3]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' to filter users.
Swapping name and email in pluck parameters.
✗ Incorrect
The operator '>' filters users older than 20, pluck gets emails as values and names as keys.