2

Here is an open bug on Github for the laravel-mongodb package, but there is no response. Maybe someone knows a solution..?

When selecting as single doc, dates displays as a date

{
    "_id": "5ca71ce0d542352064004034",
    "title": "General review",
    "status": "active",
    "start_date": "2019-04-05 09:16:16.000000",
    "end_date": "2038-01-19 03:14:07.000000",
    "start_review_date": "2019-04-05 09:16:16.000000",
    "submission_fee": 100,
    "reward": 0,
    "is_general_review_round": true,
    "created_at": "2019-04-05 09:16:16.000000",
    "updated_at": "2019-04-05 09:16:16.000000",
    "proposals": [],
    "demography": null
}

But on select as related object (nested array) I have this date conversion issue...

{
    "title": "Omnis perspiciatis aut voluptatem dolores vel repudiandae.",
    "start_date": {
        "$date": {
            "$numberLong": "1554542206000"
        }
    },
    "end_date": {
        "$date": {
            "$numberLong": "1556183806000"
        }
    },
    "start_review_date": {
        "$date": {
            "$numberLong": "1555406206000"
        }
    },
    "updated_at": {
        "$date": {
            "$numberLong": "1554455806312"
        }
    },
    "created_at": {
        "$date": {
            "$numberLong": "1554455806312"
        }
    }
}
Dees Oomens
  • 4,554
  • 3
  • 29
  • 61
Denys Siebov
  • 198
  • 4
  • 16

1 Answers1

1

Dates can be formatted from parent model: If your parent model is User and you are embedding UserDetails you could cast dates this way:

public function userDetails()
{
    return $this->embedsOne('UserDetails');
}

protected $dates = ['userDetails.created_at', 'userDetails.updated_at'];

user11197429
  • 11
  • 1
  • 1
  • this seems to be the right answer. The $dates array defines which elements should be considered of type MongoDate and transformed to Iso when read from DB – PepeNietnagel Jan 21 '21 at 12:30