Bird
Raised Fist0
Laravelframework~20 mins

CRUD with Eloquent in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which Eloquent method is used to add a new record to the database in Laravel?
easy
A. create()
B. find()
C. delete()
D. update()

Solution

  1. Step 1: Understand the purpose of create()

    The create() method in Eloquent is designed to add a new record to the database by passing an array of attributes.
  2. Step 2: Compare with other methods

    find() retrieves a record, delete() removes a record, and update() modifies an existing record. None of these add new records.
  3. Final Answer:

    create() -> Option A
  4. Quick Check:

    Add new record = create() [OK]
Hint: Use create() to add new data fast [OK]
Common Mistakes:
  • Confusing find() with create()
  • Using update() to add new records
  • Trying to delete() to add data
2. Which of the following is the correct syntax to find a record by its ID using Eloquent?
easy
A. $user = User->find($id);
B. $user = User::findById($id);
C. $user = User::find($id);
D. $user = User.find($id);

Solution

  1. Step 1: Recall Eloquent static method syntax

    Eloquent uses static methods on the model class, so User::find($id) is correct.
  2. Step 2: Identify incorrect syntax

    User->find($id) is invalid because User is a class, not an object. findById() does not exist. User.find() is not PHP syntax.
  3. Final Answer:

    $user = User::find($id); -> Option C
  4. Quick Check:

    Find by ID uses ::find() [OK]
Hint: Use double colon :: for static methods [OK]
Common Mistakes:
  • Using arrow -> on class name
  • Calling non-existent findById() method
  • Using dot . notation like JavaScript
3. What will be the output of this code snippet assuming a user with ID 5 exists?
$user = User::find(5);
if ($user) {
$user->name = 'Alice';
$user->save();
echo 'Updated';
} else {
echo 'Not found';
}
medium
A. Updated
B. Not found
C. Error: save() method missing
D. No output

Solution

  1. Step 1: find(5) execution

    Since a user with ID 5 exists, User::find(5) returns an Eloquent model instance, making $user truthy.
  2. Step 2: Update and save

    The if ($user) condition passes, so name is set to 'Alice', save() persists it to the database, and 'Updated' is echoed.
  3. Final Answer:

    Updated -> Option A
  4. Quick Check:

    Record found and saved = 'Updated' [OK]
Hint: If find() returns object, save() updates record [OK]
Common Mistakes:
  • Assuming save() is missing
  • Thinking find() throws error if no record
  • Expecting no output when record exists
4. Identify the error in this code that tries to delete a user by ID:
$user = User::find($id);
$user->delete($id);
medium
A. delete() method does not exist in Eloquent
B. find() should be replaced with findOrFail()
C. You must call save() after delete()
D. delete() should be called without arguments

Solution

  1. Step 1: Understand delete() method usage

    In Eloquent, delete() is called on the model instance without any arguments to remove that record.
  2. Step 2: Identify the incorrect argument

    The code incorrectly passes $id to delete(), which expects no parameters. This causes an error.
  3. Final Answer:

    delete() should be called without arguments -> Option D
  4. Quick Check:

    delete() takes no parameters [OK]
Hint: Call delete() directly on model instance without args [OK]
Common Mistakes:
  • Passing ID to delete() method
  • Thinking delete() needs save() after
  • Confusing delete() with static method
5. You want to update a user's email only if the user exists and the new email is not empty. Which code snippet correctly does this using Eloquent?
hard
A.
$user = User::find($id);
if ($newEmail) {
$user->email = $newEmail;
$user->save();
}
B.
$user = User::find($id);
if ($user && !empty($newEmail)) {
$user->email = $newEmail;
$user->save();
}
C.
$user = User::findOrFail($id);
$user->email = $newEmail;
$user->save();
D.
$user = User::where('id', $id)->update(['email' => $newEmail]);

Solution

  1. Step 1: Check user existence and email validity

    $user = User::find($id);
    if ($user && !empty($newEmail)) {
    $user->email = $newEmail;
    $user->save();
    }
    checks if $user exists and $newEmail is not empty before updating, preventing errors or empty updates.
  2. Step 2: Compare other options

    $user = User::find($id);
    if ($newEmail) {
    $user->email = $newEmail;
    $user->save();
    }
    skips user existence check, risking errors if user is null.
    $user = User::findOrFail($id);
    $user->email = $newEmail;
    $user->save();
    uses findOrFail() which throws exception if user missing, no email empty check.
    $user = User::where('id', $id)->update(['email' => $newEmail]);
    updates directly but does not check if $newEmail is empty.
  3. Final Answer:

    $user = User::find($id);
    if ($user && !empty($newEmail)) {
    $user->email = $newEmail;
    $user->save();
    }
    -> Option B
  4. Quick Check:

    Check user and email before save [OK]
Hint: Always check user exists and data valid before save [OK]
Common Mistakes:
  • Not checking if user exists before update
  • Skipping validation of new email
  • Using findOrFail without handling exceptions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard