Skip to content

Commit fcc0a31

Browse files
committed
Convert date-times to Laravel app's storage timezone
1 parent 92e4393 commit fcc0a31

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/Laravel/EloquentResource.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Tobyz\JsonApiServer\Resource\Paginatable;
1919
use Tobyz\JsonApiServer\Resource\Resource;
2020
use Tobyz\JsonApiServer\Resource\Updatable;
21+
use Tobyz\JsonApiServer\Schema\Field\DateTime;
2122
use Tobyz\JsonApiServer\Schema\Field\Field;
2223
use Tobyz\JsonApiServer\Schema\Field\Relationship;
2324
use Tobyz\JsonApiServer\Schema\Field\ToMany;
@@ -135,6 +136,16 @@ public function setValue(object $model, Field $field, mixed $value, Context $con
135136
return;
136137
}
137138

139+
// Mind-blowingly, Laravel discards timezone information when storing
140+
// dates in the database. Since the API can receive dates in any
141+
// timezone, we will need to convert it to the app's configured
142+
// timezone ourselves before storage.
143+
if ($field instanceof DateTime && $value instanceof \DateTimeInterface) {
144+
$value = \DateTime::createFromInterface($value)->setTimezone(
145+
new \DateTimeZone(config('app.timezone')),
146+
);
147+
}
148+
138149
$model->setAttribute($this->property($field), $value);
139150
}
140151

0 commit comments

Comments
 (0)