0
0
Laravelframework~20 mins

CRUD with Eloquent in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Eloquent CRUD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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;
AAn error because password is not fillable
BAlice
Csecret
Dnull
Attempts:
2 left
💡 Hint
Remember that the create method returns the saved model instance with attributes filled.
state_output
intermediate
2: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;
Aold email value
Bnull
CAn error because save() was not called
Dnewemail@example.com
Attempts:
2 left
💡 Hint
The save method commits changes to the database and updates the model instance.
📝 Syntax
advanced
2: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?
AUser::destroy(5);
BUser::delete(5);
CUser::find(5)->remove();
DUser::find(5)->deleteUser();
Attempts:
2 left
💡 Hint
Check the correct static method for deleting by ID in Eloquent.
🔧 Debug
advanced
2: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
ABecause save() was not called after changing the attribute
BBecause find() returns null if user not found
CBecause name is not a fillable attribute
DBecause the database connection is not configured
Attempts:
2 left
💡 Hint
Changing attributes alone does not update the database without saving.
🧠 Conceptual
expert
2: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'])?
AA syntax error occurs
BThe user is created successfully with name 'Eve'
CMassAssignmentException is thrown
DThe create method returns null
Attempts:
2 left
💡 Hint
Laravel protects against mass assignment by default.