Bird
Raised Fist0
Laravelframework~20 mins

Timestamps management in Laravel - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does the $timestamps property in a Laravel model control?
easy
A. Whether Laravel automatically manages created_at and updated_at columns
B. The timezone used for timestamps
C. The format of the timestamp columns
D. The database connection for timestamp fields

Solution

  1. Step 1: Understand the role of $timestamps in Laravel models

    The $timestamps property tells Laravel if it should automatically update created_at and updated_at fields when saving records.
  2. Step 2: Identify what $timestamps = false does

    Setting $timestamps = false disables this automatic management, so Laravel won't update those columns.
  3. Final Answer:

    Whether Laravel automatically manages created_at and updated_at columns -> Option A
  4. Quick Check:

    $timestamps controls auto timestamp updates [OK]
Hint: Remember: $timestamps toggles auto timestamp updates [OK]
Common Mistakes:
  • Confusing $timestamps with timezone settings
  • Thinking it changes timestamp format
  • Assuming it sets database connection
2. Which of the following is the correct way to add timestamp columns in a Laravel migration?
easy
A. $table->timestamp();
B. $table->timestamps();
C. $table->timeStamps();
D. $table->addTimestamps();

Solution

  1. Step 1: Recall Laravel migration syntax for timestamps

    Laravel provides a method timestamps() (all lowercase) to add created_at and updated_at columns automatically.
  2. Step 2: Check each option's syntax

    $table->timestamps(); uses the correct method name timestamps(). Options A, B, and D are either incomplete or incorrect method names.
  3. Final Answer:

    $table->timestamps(); -> Option B
  4. Quick Check:

    Migration timestamps method = timestamps() [OK]
Hint: Use exact method timestamps() in migrations [OK]
Common Mistakes:
  • Using singular timestamp() instead of plural
  • Capitalizing method name incorrectly
  • Assuming method is addTimestamps()
3. Given this model code, what will be the value of updated_at after saving a new record?
class Post extends Model {
  public $timestamps = true;
}

$post = new Post();
$post->title = 'Hello';
$post->save();
medium
A. Null because it is a new record
B. An error occurs because updated_at is not set manually
C. Timestamp of the last update before save
D. Current timestamp when save() is called

Solution

  1. Step 1: Understand $timestamps = true effect on saving

    When $timestamps is true, Laravel automatically sets created_at and updated_at to the current time on save.
  2. Step 2: Analyze saving a new record

    On creating a new record, both timestamps get the current time. So updated_at is set to now, not null or error.
  3. Final Answer:

    Current timestamp when save() is called -> Option D
  4. Quick Check:

    New record save updates timestamps automatically [OK]
Hint: New saves set both timestamps to current time [OK]
Common Mistakes:
  • Thinking updated_at is null on new records
  • Assuming manual setting is required
  • Believing an error occurs without manual timestamps
4. You have this migration snippet:
Schema::create('users', function (Blueprint $table) {
  $table->id();
  $table->string('name');
  $table->timestamps;
});

What is the problem here?
medium
A. You must add nullable() to timestamps
B. The timestamps method does not exist
C. Missing parentheses after timestamps method call
D. The id() method should come after timestamps

Solution

  1. Step 1: Check the migration method call syntax

    In Laravel migrations, timestamps() is a method and must be called with parentheses.
  2. Step 2: Identify the error in the code

    The code uses $table->timestamps; without parentheses, so it does not execute the method, causing no timestamp columns to be created.
  3. Final Answer:

    Missing parentheses after timestamps method call -> Option C
  4. Quick Check:

    Method calls need parentheses: timestamps() [OK]
Hint: Always add () when calling migration methods [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Thinking timestamps is a property, not a method
  • Assuming order of columns matters here
5. You want to disable automatic timestamp management for a model but still keep the created_at and updated_at columns in the database. How should you do this?
hard
A. Set public $timestamps = false; in the model and keep $table->timestamps() in migration
B. Remove $table->timestamps() from migration and set $timestamps = false in model
C. Set public $timestamps = true; and manually update timestamps in code
D. Rename created_at and updated_at columns to disable timestamps

Solution

  1. Step 1: Understand disabling timestamps in model

    Setting public $timestamps = false; in the model disables Laravel's automatic update of timestamp columns.
  2. Step 2: Keep timestamp columns in database

    Keeping $table->timestamps() in migration ensures the columns exist in the database even if Laravel does not update them automatically.
  3. Final Answer:

    Set public $timestamps = false; in the model and keep $table->timestamps() in migration -> Option A
  4. Quick Check:

    Disable auto timestamps in model, keep columns in migration [OK]
Hint: Disable in model, keep columns in migration to keep timestamps [OK]
Common Mistakes:
  • Removing timestamps columns from migration when disabling
  • Assuming setting $timestamps to true disables timestamps
  • Renaming columns to disable timestamps
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