Last active
July 1, 2020 17:07
A Laravel Trait to Allow temporarily disable timestamps update's.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use Illuminate\Database\Eloquent\Model; | |
trait CanIgnoreTimestamps | |
{ | |
protected static $ignoreTimestamps = false; | |
public static function ignoreTimestamps(bool $ignore = true) | |
{ | |
static::$ignoreTimestamps = $ignore; | |
} | |
public function usesTimestamps() | |
{ | |
return parent::usesTimestamps() && !static::$ignoreTimestamps; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And then...
Set flag:
MyModel::ignoreTimestamps();
Do your stuff:
MyModel::(...)
Reset the flag:
MyModel::ignoreTimestamps(false);
The $timestamps property in the model is not static so you can't use that,
but Laravel always checks the usesTimestamps() method before setting the timestamps.