Timestamps management helps you automatically track when records are created or updated in your database. This makes it easy to know the history of your data without extra work.
Timestamps management in Laravel
class ModelName extends Model {
public $timestamps = true;
}By default, Laravel models have timestamps enabled.
The timestamps use two columns: created_at and updated_at.
created_at and updated_at columns.class Post extends Model {
// timestamps are enabled by default
}class Comment extends Model {
public $timestamps = false;
}created_at and updated_at columns in the database table.Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); });
This example shows a Laravel model with timestamps enabled by default. When you save a new task, Laravel sets created_at and updated_at. When you update the task, only updated_at changes automatically.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Task extends Model { // timestamps enabled by default } // Usage example in a controller or tinker: // Creating a new task $task = new Task(); $task->name = 'Learn Laravel timestamps'; $task->save(); // The created_at and updated_at fields are set automatically // Later, updating the task $task->name = 'Master Laravel timestamps'; $task->save(); // The updated_at field updates automatically // To see timestamps: return [ 'created_at' => $task->created_at->toDateTimeString(), 'updated_at' => $task->updated_at->toDateTimeString(), ];
You can customize the timestamp column names by overriding const CREATED_AT and const UPDATED_AT in your model.
If you disable timestamps, you must manage these columns manually if needed.
Laravel uses Carbon instances for timestamps, so you can format dates easily.
Timestamps track when records are created and updated automatically.
Enable timestamps by default or disable with $timestamps = false.
Use $table->timestamps() in migrations to add timestamp columns.