@@ -20,6 +20,13 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
20
20
$ table ->integer ('created_at ' );
21
21
$ table ->integer ('updated_at ' );
22
22
});
23
+
24
+ Schema::create ('users_nullable_timestamps ' , function ($ table ) {
25
+ $ table ->increments ('id ' );
26
+ $ table ->string ('email ' )->unique ();
27
+ $ table ->timestamp ('created_at ' )->nullable ();
28
+ $ table ->timestamp ('updated_at ' )->nullable ();
29
+ });
23
30
}
24
31
25
32
protected function destroyDatabaseMigrations ()
@@ -115,6 +122,27 @@ public function testItCastTimestampsCreatedByTheBuilderWhenTimeHasPassed()
115
122
$ this ->assertSame ($ updatedAt , $ mutatorUser ->updated_at ->timestamp );
116
123
$ this ->assertSame ($ updatedAt , $ mutatorUser ->fresh ()->updated_at ->timestamp );
117
124
}
125
+
126
+ public function testItCastTimestampsUpdatedByAMutator ()
127
+ {
128
+ Carbon::setTestNow (now ());
129
+
130
+ $ mutatorUser = UserWithUpdatedAtViaMutator::create ([
131
+ 'email ' => fake ()->unique ()->email ,
132
+ ]);
133
+
134
+ $ this ->assertNull ($ mutatorUser ->updated_at );
135
+
136
+ Carbon::setTestNow (now ()->addSecond ());
137
+ $ updatedAt = now ()->timestamp ;
138
+
139
+ $ mutatorUser ->update ([
140
+ 'email ' => fake ()->unique ()->email ,
141
+ ]);
142
+
143
+ $ this ->assertSame ($ updatedAt , $ mutatorUser ->updated_at ->timestamp );
144
+ $ this ->assertSame ($ updatedAt , $ mutatorUser ->fresh ()->updated_at ->timestamp );
145
+ }
118
146
}
119
147
120
148
class UserWithIntTimestampsViaCasts extends Model
@@ -191,3 +219,19 @@ protected function setCreatedAtAttribute($value)
191
219
$ this ->attributes ['created_at ' ] = Carbon::parse ($ value )->timestamp ;
192
220
}
193
221
}
222
+
223
+ class UserWithUpdatedAtViaMutator extends Model
224
+ {
225
+ protected $ table = 'users_nullable_timestamps ' ;
226
+
227
+ protected $ fillable = ['email ' , 'updated_at ' ];
228
+
229
+ public function setUpdatedAtAttribute ($ value )
230
+ {
231
+ if (! $ this ->id ) {
232
+ return ;
233
+ }
234
+
235
+ $ this ->updated_at = $ value ;
236
+ }
237
+ }
0 commit comments