Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
3c0edb03
Commit
3c0edb03
authored
Apr 14, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweaking date format for json output
parent
c2fb55c4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
2 deletions
+28
-2
Model.php
src/Jenssegers/Mongodb/Model.php
+11
-1
ModelTest.php
tests/ModelTest.php
+12
-1
User.php
tests/models/User.php
+5
-0
No files found.
src/Jenssegers/Mongodb/Model.php
View file @
3c0edb03
...
...
@@ -136,6 +136,16 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return
Carbon
::
instance
(
$value
);
}
/**
* Get the format for database stored dates.
*
* @return string
*/
protected
function
getDateFormat
()
{
return
'Y-m-d H:i:s'
;
}
/**
* Get a fresh timestamp for the model.
*
...
...
@@ -206,7 +216,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
if
(
$value
instanceof
MongoDate
)
{
$value
=
$this
->
asDateTime
(
$value
)
->
format
(
'Y-m-d H:i:s'
);
$value
=
$this
->
asDateTime
(
$value
)
->
format
(
$this
->
getDateFormat
()
);
}
}
...
...
tests/ModelTest.php
View file @
3c0edb03
...
...
@@ -318,7 +318,8 @@ class ModelTest extends TestCase {
public
function
testDates
()
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'birthday'
=>
new
DateTime
(
'1980/1/1'
)));
$birthday
=
new
DateTime
(
'1980/1/1'
);
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'birthday'
=>
$birthday
));
$this
->
assertInstanceOf
(
'Carbon\Carbon'
,
$user
->
birthday
);
$check
=
User
::
find
(
$user
->
_id
);
...
...
@@ -327,6 +328,16 @@ class ModelTest extends TestCase {
$user
=
User
::
where
(
'birthday'
,
'>'
,
new
DateTime
(
'1975/1/1'
))
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
// test custom date format for json output
$json
=
$user
->
toArray
();
$this
->
assertEquals
(
$user
->
birthday
->
format
(
'U'
),
$json
[
'birthday'
]);
$this
->
assertEquals
(
$user
->
created_at
->
format
(
'U'
),
$json
[
'created_at'
]);
// test default date format for json output
$item
=
Item
::
create
(
array
(
'name'
=>
'sword'
));
$json
=
$item
->
toArray
();
$this
->
assertEquals
(
$item
->
created_at
->
format
(
'Y-m-d H:i:s'
),
$json
[
'created_at'
]);
}
public
function
testIdAttribute
()
...
...
tests/models/User.php
View file @
3c0edb03
...
...
@@ -84,4 +84,9 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{
return
$this
->
email
;
}
protected
function
getDateFormat
()
{
return
'U'
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment