Sharath Sharath - 1 year ago 184
PHP Question

How to store Carbon DateTime ISO object in Sub Json Document in Laravel 5.2 MongoDB

I am trying to store Carbon datetime in Sub Json Document but instead of storing in ISO format its storing in different way shown below.

"_id" : ObjectId("5791bc3d6e79d211aa5be019"),
"breakUps" : [
"amount" : 2000,
"expiry" : {
"date" : "2016-09-05 06:25:01.000000",
"timezone_type" : 3,
"timezone" : "UTC"
"updated_at" : ISODate("2016-07-22T06:25:01.576Z"),
"created_at" : ISODate("2016-07-22T06:25:01.576Z")

Instead it should have been stored like ISODate("2016-07-22T06:25:01.576Z") format.

My Eloquent Model class is:

class Configurations extends Eloquent
public $incrementing = false;
public $timestamps = true;
protected $collection = 'configurations';
protected $dates = array('created_at','updated_at','breakUps.expiry');

PHP Code which i am using for Saving the Object

$config1 = new Configurations();
$config1->breakUps = [['amount' => 1000, 'expiry' => Carbon::now()->addDays(30)]];

Answer Source

Date mutators in Laravel do not support dot notation and hence,

protected $dates = ['breakUps.expiry'];

just won't work. One simple way to solve this problem is to move the key to the root of the document as breakUps_expiry.

protected $dates = ['breakUps_expiry'];

There is already a issue logged on laravel-mongodb github page, you can track it here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download