File tree Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -136,6 +136,16 @@ protected function asDateTime($value)
136
136
return Carbon::instance ($ value );
137
137
}
138
138
139
+ /**
140
+ * Get the format for database stored dates.
141
+ *
142
+ * @return string
143
+ */
144
+ protected function getDateFormat ()
145
+ {
146
+ return 'Y-m-d H:i:s ' ;
147
+ }
148
+
139
149
/**
140
150
* Get a fresh timestamp for the model.
141
151
*
@@ -206,7 +216,7 @@ public function attributesToArray()
206
216
*/
207
217
if ($ value instanceof MongoDate)
208
218
{
209
- $ value = $ this ->asDateTime ($ value )->format (' Y-m-d H:i:s ' );
219
+ $ value = $ this ->asDateTime ($ value )->format ($ this -> getDateFormat () );
210
220
}
211
221
}
212
222
Original file line number Diff line number Diff line change @@ -318,7 +318,8 @@ public function testUnset()
318
318
319
319
public function testDates ()
320
320
{
321
- $ user = User::create (array ('name ' => 'John Doe ' , 'birthday ' => new DateTime ('1980/1/1 ' )));
321
+ $ birthday = new DateTime ('1980/1/1 ' );
322
+ $ user = User::create (array ('name ' => 'John Doe ' , 'birthday ' => $ birthday ));
322
323
$ this ->assertInstanceOf ('Carbon\Carbon ' , $ user ->birthday );
323
324
324
325
$ check = User::find ($ user ->_id );
@@ -327,6 +328,16 @@ public function testDates()
327
328
328
329
$ user = User::where ('birthday ' , '> ' , new DateTime ('1975/1/1 ' ))->first ();
329
330
$ this ->assertEquals ('John Doe ' , $ user ->name );
331
+
332
+ // test custom date format for json output
333
+ $ json = $ user ->toArray ();
334
+ $ this ->assertEquals ($ user ->birthday ->format ('U ' ), $ json ['birthday ' ]);
335
+ $ this ->assertEquals ($ user ->created_at ->format ('U ' ), $ json ['created_at ' ]);
336
+
337
+ // test default date format for json output
338
+ $ item = Item::create (array ('name ' => 'sword ' ));
339
+ $ json = $ item ->toArray ();
340
+ $ this ->assertEquals ($ item ->created_at ->format ('Y-m-d H:i:s ' ), $ json ['created_at ' ]);
330
341
}
331
342
332
343
public function testIdAttribute ()
Original file line number Diff line number Diff line change @@ -84,4 +84,9 @@ public function getReminderEmail()
84
84
{
85
85
return $ this ->email ;
86
86
}
87
+
88
+ protected function getDateFormat ()
89
+ {
90
+ return 'U ' ;
91
+ }
87
92
}
You can’t perform that action at this time.
0 commit comments