Skip to content

Instantly share code, notes, and snippets.

@stefanzweifel
Last active July 29, 2017 14:18
Show Gist options
  • Save stefanzweifel/84443804ed34c2f674baaae4abb81f45 to your computer and use it in GitHub Desktop.
Save stefanzweifel/84443804ed34c2f674baaae4abb81f45 to your computer and use it in GitHub Desktop.
A PHP Trait for Laravel Applications to disable the `updated_at` attribute on Models. It should be used if you only have a `created_at` column but not a `updated_at` column. https://stefanzweifel.io/posts/disableupdatedat-trait-for-laravel-5-4
<?php
namespace App\Traits;
/**
* This Trait disabled the updated_at value for a model.
* Laravel doesn't provide an easy way to to this.
* The framework **alyways** tries to append the updated_at column
* when the `$timestamps` property is not set to false.
* See code here: https://github.com/illuminate/database/blob/5.4/Eloquent/Builder.php#L757-L760.
*
* This traits disables all timestamps (including created_at) when performing
* an update. We do this by listening to the `updating` and `updated`
* events.
*/
trait DisableUpdatedAt
{
protected static function boot()
{
parent::boot();
static::updating(function ($model) {
$model->timestamps = false;
});
static::updated(function ($model) {
$model->timestamps = true;
});
}
public function setUpdatedAt($value)
{
//
}
public function getUpdatedAtColumn()
{
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment