0
0
Laravelframework~10 mins

Timestamps management in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Timestamps management
Model Created
Check if timestamps enabled?
NoSkip timestamp update
Yes
Set created_at and updated_at to current time
Save model to database
On Model Update
Check if timestamps enabled?
NoSkip timestamp update
Yes
Set updated_at to current time
Save changes to database
When a Laravel model is created or updated, it checks if timestamps are enabled. If yes, it sets created_at and updated_at fields automatically before saving.
Execution Sample
Laravel
use Illuminate\Database\Eloquent\Model;

class Post extends Model {
  public $timestamps = true;
}

$post = new Post();
$post->save();
This code creates a new Post model instance and saves it, automatically setting timestamps if enabled.
Execution Table
StepActionTimestamps Enabled?created_atupdated_atDatabase Save
1Create new Post instancetruenullnullNo
2Check timestamps enabledtruenullnullNo
3Set created_at and updated_at to nowtrue2024-06-01 12:00:002024-06-01 12:00:00No
4Save Post to databasetrue2024-06-01 12:00:002024-06-01 12:00:00Yes
5Update Post instancetrue2024-06-01 12:00:002024-06-01 12:00:00No
6Check timestamps enabledtrue2024-06-01 12:00:002024-06-01 12:00:00No
7Set updated_at to nowtrue2024-06-01 12:00:002024-06-01 12:05:00No
8Save updated Post to databasetrue2024-06-01 12:00:002024-06-01 12:05:00Yes
9Create new Post with timestamps disabledfalsenullnullNo
10Check timestamps enabledfalsenullnullNo
11Skip timestamp settingfalsenullnullNo
12Save Post to databasefalsenullnullYes
💡 Execution stops after saving models; timestamps are set only if enabled.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 7After Step 8After Step 11Final
created_atnull2024-06-01 12:00:002024-06-01 12:00:002024-06-01 12:00:002024-06-01 12:00:00nullnull
updated_atnull2024-06-01 12:00:002024-06-01 12:00:002024-06-01 12:05:002024-06-01 12:05:00nullnull
timestamps_enabledtruetruetruetruetruefalsefalse
Key Moments - 3 Insights
Why are created_at and updated_at set to the same time when creating a new model?
When creating a new model, Laravel sets both created_at and updated_at to the current time because the record is new and just created. See execution_table rows 3 and 4.
What happens if timestamps are disabled on a model?
If timestamps are disabled (timestamps_enabled is false), Laravel skips setting created_at and updated_at fields before saving. See execution_table rows 9 to 12.
Why does updated_at change on update but created_at stays the same?
created_at marks when the record was first created and does not change. updated_at updates every time the record changes. See execution_table rows 7 and 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What are the values of created_at and updated_at?
A"2024-06-01 12:00:00" for both
B"null" for both
C"2024-06-01 12:05:00" for both
D"2024-06-01 12:00:00" for created_at and "null" for updated_at
💡 Hint
Check the 'created_at' and 'updated_at' columns in execution_table row 3.
At which step does the updated_at timestamp get updated after the initial creation?
AStep 4
BStep 3
CStep 7
DStep 11
💡 Hint
Look at the 'updated_at' column changes in execution_table rows 7 and 8.
If timestamps are disabled, what happens to the created_at and updated_at fields when saving?
AThey are set to current time
BThey remain null
COnly updated_at is set
Dcreated_at is set, updated_at remains null
💡 Hint
See execution_table rows 9 to 12 where timestamps_enabled is false.
Concept Snapshot
Laravel models have automatic timestamps: created_at and updated_at.
When $timestamps = true (default), Laravel sets these fields on create and update.
created_at is set once on creation; updated_at updates on every save.
Disable timestamps by setting public $timestamps = false in the model.
Timestamps help track when records were created and last changed.
Full Transcript
In Laravel, models can automatically manage timestamps for creation and updates. When you create a new model instance and save it, Laravel checks if timestamps are enabled. If yes, it sets both created_at and updated_at fields to the current time before saving. When you update the model later, only updated_at changes to the new current time, while created_at remains the same. If you disable timestamps by setting public $timestamps = false in your model, Laravel skips setting these fields. This helps you track when records were created and last updated without manual work.