laravel-mongodb
laravel-mongodb copied to clipboard
Nested date format is different from top level in response
- Laravel-mongodb Version: 3.7
- PHP Version: 7.4.9
Steps to reproduce
- create model with propery "nested.date"
- return it as JsonResource for example
- you will date in "2020-10-23 09:12:19" format where other dates in "2020-10-23T09:12:19.512000Z"
It looks like $this->serializeDate() should be called in Mongodb/Eloquent/Model.php#L219 in order to mimic Eloquent's addDateAttributesToArray() in Eloquent/Concerns/HasAttributes.php#L168 trait
Hello,
I see that you've already debugged and found resolution, could you please push a new PR with tests?
Thanks!
Will try later. Also there is another related bug: new object of such Model fails
[2020-10-23 10:50:25] production.ERROR: invalid document for insert: keys cannot contain ".": "last_request.date" {"exception":"[object] (MongoDB\\Driver\\Exception\\InvalidArgumentException(code: 22): invalid document for insert: keys cannot contain \".\": \"last_request.date\" at /src/vendor/mongodb/mongodb/src/Operation/InsertOne.php:132)
in my case last_request is null and there is no nested date, didn't dig deep into this, added this overrides on my model as temporary solution
// Skip setting dotted dates as attribute for new instance
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
foreach ($this->getDates() as $key) {
if (Str::contains($key, '.')) {
unset($this->attributes[$key]);
}
}
}
// Avoid issue with formatting nested dates
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($this->getDates() as $key) {
if (Str::contains($key, '.') && Arr::has($attributes, $key)) {
Arr::set($attributes, $key, $this->serializeDate($this->asDateTime(Arr::get($attributes, $key))));
}
}
return $attributes;
}
I'm having the same issue, was it merged so far?
I'm having the same issue, was it merged so far?
No, that's the reason why this issue is still open.
Thanks.
@divine, question about local tests: readme says that all you need is to run "docker-compose up", but it doesn't work to me w/o 'command: sh -c "composer install --no-interaction && /code/vendor/bin/phpunit"'on "tests" image in docker-compose.yml - did I miss something?
Regardless https://github.com/jenssegers/laravel-mongodb/issues/2129#issuecomment-715281885 - I'm unable to reproduce this for now, will open separate issue if this happens again.
Thanks.
Hello @alexsmko ,
Yes, you're right indeed, it doesn't start the test. Docker compose file needs to be updated as well, would you like to help? 😉
Thanks!
This is still an issue:
- Whenever I try to set a field that's not included in
$dates
array withCarbon
value, it results with an empty object - When I try to add
nested.date
field and set it to aCarbon
value, it's also saved as an empty object
Any workaround? Sometthing I might be missing?