Challenge - 5 Problems
Eloquent CRUD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Eloquent create operation?
Consider the following Laravel Eloquent code that creates a new user record. What will be the value of
$user->name after running this code?Laravel
<?php $user = User::create(['name' => 'Alice', 'email' => 'alice@example.com', 'password' => bcrypt('secret')]); echo $user->name;
Attempts:
2 left
💡 Hint
Remember that the create method returns the saved model instance with attributes filled.
✗ Incorrect
The create method saves the record and returns the model instance with all attributes accessible. Since 'name' is set to 'Alice', echoing $user->name outputs 'Alice'.
❓ state_output
intermediate2:00remaining
What is the result of this update operation on Eloquent model?
Given this code snippet, what will be the value of
$user->email after the update?Laravel
<?php $user = User::find(1); $user->email = 'newemail@example.com'; $user->save(); echo $user->email;
Attempts:
2 left
💡 Hint
The save method commits changes to the database and updates the model instance.
✗ Incorrect
After setting the email attribute and calling save(), the model instance reflects the updated email. So echoing $user->email outputs the new email.
📝 Syntax
advanced2:00remaining
Which option correctly deletes a user by ID using Eloquent?
You want to delete a user with ID 5. Which code snippet will successfully delete the user without errors?
Attempts:
2 left
💡 Hint
Check the correct static method for deleting by ID in Eloquent.
✗ Incorrect
The destroy method deletes records by primary key. delete() is an instance method, not static. remove() and deleteUser() do not exist.
🔧 Debug
advanced2:00remaining
Why does this Eloquent update not change the database?
Look at this code snippet. The user's name is changed but the database does not update. What is the cause?
Laravel
<?php $user = User::find(10); $user->name = 'Bob'; // Missing save call
Attempts:
2 left
💡 Hint
Changing attributes alone does not update the database without saving.
✗ Incorrect
Eloquent requires calling save() to persist changes. Without save(), changes remain only in the model instance.
🧠 Conceptual
expert2:00remaining
What error occurs when trying to create a model without fillable attributes set?
Given a Laravel model with no
$fillable or $guarded properties defined, what happens when you run User::create(['name' => 'Eve'])?Attempts:
2 left
💡 Hint
Laravel protects against mass assignment by default.
✗ Incorrect
Without defining fillable or guarded, Laravel blocks mass assignment and throws a MassAssignmentException to prevent unsafe data insertion.