Challenge - 5 Problems
Timestamp Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Laravel handle automatic timestamps in Eloquent models?
Consider a Laravel Eloquent model with default settings. What happens to the
created_at and updated_at fields when you save a new model instance?Laravel
class Post extends Model { // No changes to timestamps } $post = new Post(); $post->title = 'Hello'; $post->save(); return [$post->created_at, $post->updated_at];
Attempts:
2 left
💡 Hint
Think about Laravel's default behavior for timestamps in models.
✗ Incorrect
By default, Laravel automatically sets both created_at and updated_at to the current time when a new model is saved.
📝 Syntax
intermediate2:00remaining
How to disable automatic timestamps in a Laravel model?
You want to stop Laravel from automatically managing
created_at and updated_at in your model. Which code snippet correctly disables this feature?Laravel
class Comment extends Model {
// Your code here
}Attempts:
2 left
💡 Hint
Check the visibility and naming of the property controlling timestamps.
✗ Incorrect
To disable timestamps, you set the public property $timestamps to false in your model.
🔧 Debug
advanced2:00remaining
Why is
updated_at not updating on model save?You have a Laravel model with timestamps enabled. You update a field and call
save(), but updated_at does not change. What is the most likely cause?Laravel
class Article extends Model { // timestamps enabled by default } $article = Article::find(1); $article->title = 'New Title'; $article->save(); // updated_at remains unchanged
Attempts:
2 left
💡 Hint
Laravel only updates
updated_at if the model detects changes.✗ Incorrect
Laravel updates updated_at only if the model's attributes have changed. If the value assigned is the same as before, updated_at stays the same.
🧠 Conceptual
advanced2:00remaining
What is the effect of customizing the timestamp column names in Laravel?
You want to use
createdOn and updatedOn instead of the default created_at and updated_at. What must you do in your model to make Laravel handle these custom names?Laravel
class Event extends Model {
// Your code here
}Attempts:
2 left
💡 Hint
Laravel allows changing timestamp column names by overriding constants.
✗ Incorrect
Laravel lets you customize timestamp column names by defining CREATED_AT and UPDATED_AT constants in your model.
❓ state_output
expert2:00remaining
What is the output of this Laravel model timestamp manipulation code?
Given the following code, what will be the output of
return $post->updated_at->diffInMinutes($post->created_at);?Laravel
use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; class Post extends Model { public $timestamps = true; } $post = new Post(); $post->created_at = Carbon::now()->subMinutes(10); $post->updated_at = Carbon::now(); return $post->updated_at->diffInMinutes($post->created_at);
Attempts:
2 left
💡 Hint
Carbon's diffInMinutes returns the difference in minutes between two dates.
✗ Incorrect
The code sets created_at to 10 minutes ago and updated_at to now. The difference in minutes is 10.