Disable Laravel's Eloquent timestamps
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Disable Laravel's Eloquent Timestamps Without Modifying Core Files
Introduction: Incorporating timestamps into your database tables is an essential aspect of modern web development, as it helps track the changes and activities performed by users. However, sometimes you might have additional logging mechanisms already in place or do not want to add timestamp columns for various reasons. Laravel's Eloquent offers a simple method to disable timestamps across all models without requiring any core file modification. In this comprehensive guide, we will explore the different approaches that can be used to achieve these results while maintaining the best coding practices and keeping your application up-to-date.
I. Using Model Traits:
The Laravel framework allows you to create custom model traits containing various functionalities related to timestamps. To disable timestamps across all models, simply follow these steps:
1. Create a new trait named 'DisableTimestamps' under the app/Models directory or wherever suits your project structure. Alternatively, you can place this in a shared/common location accessible for other projects if you are using a package manager like Composer.
2. Add the following code to the newly created file:
```php
getRawAttributes())) {
$model->getDateAttributes() = [];
}
});
}
}
```
4. This code listens for the "eloquent.saving:before" event and ensures that any existing model attributes are empty before setting the timestamps, essentially stopping them from being populated. The code prevents Laravel's default behaviour of adding timestamps when performing a save operation on any model.
Conclusion: Disabling Laravel's Eloquent timestamps across all models can be achieved without modifying core files or requiring every model to include custom traits or traits from external packages. By using global hooks, you maintain the integrity of your application while still having control over your database timestamps as needed. The solution provided here is flexible and easily adaptable for a wide range of projects. Remember always to follow best practices to ensure optimal performance and scalability in your Laravel application.