0
0
Laravelframework~20 mins

Timestamps management in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timestamp Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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];
AOnly <code>created_at</code> is set automatically; <code>updated_at</code> remains null.
BBoth <code>created_at</code> and <code>updated_at</code> are set to the current timestamp automatically.
CNeither <code>created_at</code> nor <code>updated_at</code> are set automatically; you must set them manually.
DOnly <code>updated_at</code> is set automatically; <code>created_at</code> remains null.
Attempts:
2 left
💡 Hint
Think about Laravel's default behavior for timestamps in models.
📝 Syntax
intermediate
2: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
}
Apublic $timestamps = false;
Bprotected $timestamps = false;
Cpublic $timestamps = true;
Dprotected $timestamps = true;
Attempts:
2 left
💡 Hint
Check the visibility and naming of the property controlling timestamps.
🔧 Debug
advanced
2: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
AThe model's <code>title</code> attribute was not actually changed, so Laravel skips updating <code>updated_at</code>.
BThe <code>save()</code> method was not called correctly.
CThe <code>updated_at</code> column is missing from the database table.
DTimestamps are disabled in the model.
Attempts:
2 left
💡 Hint
Laravel only updates updated_at if the model detects changes.
🧠 Conceptual
advanced
2: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
}
AUse <code>protected $timestamps = ['createdOn', 'updatedOn'];</code> in the model.
BSet <code>public $timestamps = false;</code> and manage timestamps manually.
COverride <code>const CREATED_AT = 'createdOn';</code> and <code>const UPDATED_AT = 'updatedOn';</code> in the model.
DRename the database columns to <code>created_at</code> and <code>updated_at</code> only.
Attempts:
2 left
💡 Hint
Laravel allows changing timestamp column names by overriding constants.
state_output
expert
2: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);
AReturns null because timestamps are not saved
B0
CThrows an error because timestamps are manually set
D10
Attempts:
2 left
💡 Hint
Carbon's diffInMinutes returns the difference in minutes between two dates.