0
0
Laravelframework~20 mins

Tinker for database interaction in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tinker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output of this Tinker command?
You run this command in Laravel Tinker:
App\Models\User::where('email', 'admin@example.com')->exists();
What does this command return if a user with that email exists?
Atrue
Bfalse
CThe user model instance
DAn array of users
Attempts:
2 left
💡 Hint
Think about what the 'exists()' method returns in Laravel queries.
📝 Syntax
intermediate
1:30remaining
Which Tinker command correctly updates a user's name?
You want to update the name of the user with id 5 to 'Alice' using Laravel Tinker. Which command is correct?
AApp\Models\User::find(5)->name = 'Alice'; App\Models\User::update();
BApp\Models\User::find(5)->update(['name' => 'Alice']);
CApp\Models\User::find(5)->name = 'Alice'; App\Models\User::save();
DApp\Models\User::where('id', 5)->update('name', 'Alice');
Attempts:
2 left
💡 Hint
Remember how to update attributes and save changes in Eloquent models.
🔧 Debug
advanced
2:00remaining
Why does this Tinker command cause an error?
You run:
$user = App\Models\User::find(10); $user->email = 'new@example.com';
But the email does not update in the database. Why?
ABecause Tinker does not allow updating model attributes.
BBecause the find method does not return a model instance.
CBecause the email attribute is protected and cannot be changed.
DBecause you forgot to call $user->save() after changing the email.
Attempts:
2 left
💡 Hint
Think about how Eloquent saves changes to the database.
state_output
advanced
1:30remaining
What is the output of this Tinker code?
Run this in Laravel Tinker:
$count = App\Models\Post::where('published', true)->count(); echo $count;
What does it output?
AA syntax error
BAn array of posts
CThe number of posts where 'published' is true
Dtrue
Attempts:
2 left
💡 Hint
The count() method returns a number, not a boolean or array.
🧠 Conceptual
expert
2:30remaining
Which Tinker command will delete all users created before 2020 safely?
You want to delete all users created before 2020 using Laravel Tinker. Which command is the best to avoid accidental mass deletion?
AApp\Models\User::where('created_at', '<', '2020-01-01')->delete();
BApp\Models\User::all()->where('created_at', '<', '2020-01-01')->delete();
CApp\Models\User::truncate();
DDB::table('users')->delete();
Attempts:
2 left
💡 Hint
Consider which method deletes only the targeted records safely.