diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 79282e19e884..129a96b4762c 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -294,6 +294,10 @@ public static function isIgnoringTouch($class = null) { $class = $class ?: static::class; + if (! get_class_vars($class)['timestamps'] || ! $class::UPDATED_AT) { + return true; + } + foreach (static::$ignoreOnTouch as $ignoredClass) { if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) { return true; diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 8a07eeabecb1..165a3d63ea97 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1896,6 +1896,27 @@ protected function addMockConnection($model) return new BaseBuilder($connection, $grammar, $processor); }); } + + public function testTouchingModelWithTimestamps() + { + $this->assertFalse( + Model::isIgnoringTouch(Model::class) + ); + } + + public function testNotTouchingModelWithUpdatedAtNull() + { + $this->assertTrue( + Model::isIgnoringTouch(EloquentModelWithUpdatedAtNull::class) + ); + } + + public function testNotTouchingModelWithoutTimestamps() + { + $this->assertTrue( + Model::isIgnoringTouch(EloquentModelWithoutTimestamps::class) + ); + } } class EloquentTestObserverStub @@ -2303,3 +2324,15 @@ class EloquentModelEventObjectStub extends Model 'saving' => EloquentModelSavingEventStub::class, ]; } + +class EloquentModelWithoutTimestamps extends Model +{ + protected $table = 'stub'; + public $timestamps = false; +} + +class EloquentModelWithUpdatedAtNull extends Model +{ + protected $table = 'stub'; + const UPDATED_AT = null; +}