0
0
Laravelframework~10 mins

Why Eloquent simplifies database operations 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 retrieve all users using Eloquent.

Laravel
$users = User::[1]();
Drag options to blanks, or click blank then click option'
Aall
Bget
Cfetch
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
User::get() also returns a collection of all Eloquent models, but all() is the dedicated shortcut.
'fetch' does not exist, and 'select()' specifies columns but requires ->get() to retrieve records.
2fill in blank
medium

Complete the code to find a user by their primary key using Eloquent.

Laravel
$user = User::[1](1);
Drag options to blanks, or click blank then click option'
Afind
Bfirst
Cget
Dwhere
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'where' requires additional method calls to get the result.
Using 'get' returns a collection, not a single model.
User::first() gets the first record, not by specific ID; use where('id', 1)->first() instead.
3fill in blank
hard

Fix the error in the code to create and save a new user with Eloquent.

Laravel
$user = new User;
$user->name = 'Alice';
$user->email = 'alice@example.com';
$user->[1]();
Drag options to blanks, or click blank then click option'
Acreate
Binsert
Cupdate
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' is a query builder method, not for model instances.
Using 'create' requires mass assignment and static call.
Using 'update' is for modifying existing records, not new ones.
4fill in blank
hard

Fill both blanks to get users whose age is greater than 18 using Eloquent.

Laravel
$adults = User::where('[1]', '[2]', 18)->get();
Drag options to blanks, or click blank then click option'
Aage
B>
C<
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will get users younger than 18.
Using 'name' instead of 'age' filters by the wrong column.
5fill in blank
hard

Fill all three blanks to create a dictionary of user emails and names for users older than 25.

Laravel
$result = User::where('[1]', '[2]', 25)->pluck('[3]', 'email');
Drag options to blanks, or click blank then click option'
Aage
B>
Cname
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'email' instead of 'name' for the values will swap keys and values.
Using '<' instead of '>' will filter the wrong users.