How to disable/enable timestamps in Laravel Eloquent

Updated: January 17, 2024 By: Guest Contributor Post a comment

Introduction

Laravel Eloquent is a powerful ORM that simplifies data manipulation in database tables linked to your models. By default, Eloquent expects created_at and updated_at columns to exist on your tables, which it automatically manages, reflecting the creation and update timestamps of your model instances. In this guide, we will explore how you can take control of these timestamps, either disabling them when not needed or enabling them according to your application requirements.

Disabling Timestamps in Eloquent

When dealing with models in Laravel, Eloquent can automatically set created_at and updated_at fields during insert or update operations. This behavior is not only convenient but also standard practice to track when records are added or modified. However, there may be certain scenarios where these timestamps are not required, or you may want to manage them manually.

Disabling Timestamps Globally

If you do not require created_at and updated_at columns on any of your models, you can disable timestamps globally by setting the $timestamps property to false in your base model which is typically the App\Models\Model class in Laravel 8 and above.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model as BaseModel;

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

All models extending this base model will inherit the timestamp behavior and thus not use automated timestamping.

Disabling Timestamps per Model

To turn off automatic timestamps on a per-model basis, you can set the $timestamps property to false in individual model classes. For instance:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

With timestamps disabled, you are responsible for setting the values of these timestamp fields manually (if they exist in your table) or forgoing them altogether.

Enabling Timestamps

To reenable timestamps in a model that has them turned off, set the $timestamps property to true. This relinquishes control back to Eloquent for managing timestamp fields.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

Custom Timestamp Column Names

In some situations, you might want to use different column names for your timestamps. Eloquent allows you to customize these column names by setting the const CREATED_AT and const UPDATED_AT in your model. Here’s how:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'last_modified';
}

This tells Eloquent to use creation_date and last_modified instead of the default created_at and updated_at when performing automatic timestamping.

Advanced Use Cases

Now let’s explore some advanced scenarios. Laravel allows you to manipulate timestamps in more refined ways, such as setting the timestamps directly when creating or updating models, or preventing the updated_at field from changing during mass updates.

To set the timestamps directly:

$post = new App\Models\Post([
    // other attributes
    'creation_date' => now(),
    'last_modified' => now(),
]);

$post->save();

To prevent updated_at from changing during a mass update:

App\Models\Post::where('active', 1)->update([
    'status' => 'archived',
], ['timestamps' => false]);

Note how we pass ['timestamps' => false] as a second parameter to the update method, which tells Eloquent not to touch the timestamp columns for this operation.

Troubleshooting

Sometimes, you might encounter issues with timestamps, such as a NULL value being inserted for your timestamp columns despite having $timestamps set to true, or Eloquent not recognizing your custom timestamp columns. In such cases, ensure your database table structure matches your model’s expectations and double-check your model properties and constants for typos.

Conclusion

In this tutorial, we covered how to enable and disable automatic timestamp handling in Laravel’s Eloquent ORM. We walked through the global settings, per-model settings, custom column names, troubleshooting tips, and advanced use cases. Taking control of timestamp behavior in Eloquent can help you fine-tune the functionality of your Laravel applications.