Laravel Eloquent Model Timestamps

Eloquent Models Eloquent Relationships Laravel Migrations Laravel Eloquent Model Timestamps

Migrations: creating timestamps columns

The timestamps() method creates created_at and updated_at TIMESTAMP equivalent columns.

Schema::create('articles', function (Blueprint $table) {
	$table->id();
	$table->string('name');
	$table->timestamps(); 
});

Creates created_at and updated_at columns.

$table->timestamps();

Customize the names of the timestamps columns

class Article extends Model
{
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'updated_date';
}

Creates created_at and updated_at columns with timezone.

$table->timestampsTz();

Create created_at and updated_at columns with an optional millisecond precision (total digits).

$table->timestamps($precision = 0); 

Creates custom added_at TIMESTAMP column.

$table->timestamp('added_at', $precision = 0);

Migrations: drop timestamps columns

Drop the created_at and updated_at columns.

$table->dropTimestamps();	

Eloquent: creating and updating values for timestamps' columns

Creating created_at and updated_at values

When models are created or updated, Eloquent will automatically set the values of these columns.

Updating updated_at value

Column updated_at will be updated automatically if you use Eloquent's save method.

$article->name = 'New Custom Name';
$article->save();

Column updated_at will not be updated if you use update method. You must update this field manually

$article->update([
    'name' => 'New Custom Name',
    'updated_at' => Carbon::now()
]); 

Updating updated_at mannualy

You can also update timestamps using this touch method. Column updated_at will be updated.

$article->touch();

Update model without touching updated_at

$article->name = 'New Custom Name';
$article->timestamps = false;
$article->save();

Eloquent: Conventions

If you have not created created_at, or updated_at columns in the table then you must specify this in the model. Otherwise Eloquent will try to update these fields.

class Article extends Model
{
    public $timestamps = false;
}

Set the dateFormat property on your model to alter the format of your model's timestamps.

class Article extends Model
{
    protected $dateFormat = 'U';
}

You can use the CREATED AT and UPDATED AT variables on your model to alter the names of the columns used to store timestamps.

class Article extends Model
{
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'updated_date';
}

Eloquent: Relationships

Intermediate Table Columns

If you want your intermediate table to have created_at and updated_at timestamps that are automatically maintained by Eloquent, call the withTimestamps method when defining the relationship

class Articles extends Model
{
    public function tags() : BelongsToMany
    {
        return $this->belongsToMany(Tag::class)->withTimestamps();
    }
}

Touching Parent Timestamps

When a model has a belongsTo or belongsToMany relationship with another model, such as a Comment belonging to an Article, it can be useful to update the parent's timestamp when the child model is updated.

class Comment extends Model
{
    protected $touches = ['article'];
     
    public function article()
    {
        return $this->belongsTo(Article::class);
    }
}

Parent model timestamps will only be updated if the child model is updated using Eloquent's save method.

Touching Parent Timestamps doesn't work for ManyToMany relationships.

Eloquent: sorting by timestamps' columns

Sorting by created_at asc

Article::orderBy('created_at')->get();
// or
Article::oldest()->get();

Sorting by created_at desc

Article::orderBy('created_at', 'desc')->get();
// or
Article::latest()->get();