Halfpint
Halfpint

Reputation: 4077

Mongo date displays as null when querying with Laravel

In one of my Mongo collections I have a startedAt property on each Document, if I send an AJAX request to get the document directly from Mongo the date comes back as a normal Date object and I can use it as expected, however if I try and query from Laravel, I get a UTCDateTime object that has nothing in it, if I try to print dd it I simply get null printed.

Below is the query in my controller that I send to Mongo:

public function player($accountId)
{
    $skip = 0;
    $take = 10;

    if(isset($_GET['skip'])) $skip = (int)$_GET['skip'];
    if(isset($_GET['take'])) $take = (int)$_GET['take'];

    $matches = Match::where(['players' => array('$elemMatch' => array('accountId' => $accountId))])
        ->skip($skip)
        ->take($take)
        ->get();

    return $matches;
}

Every other field in the collection is accessible in blade, but startedAt always returns null. I've inspected in Mongo Chef and all dates are being saved as Date and they are all in a format like: 2016-08-10T00:15:14.251Z. Why is Laravel unable to process the mongo date, or am I missing something blatantly obvious here?

Upvotes: 0

Views: 595

Answers (1)

Edward Louth
Edward Louth

Reputation: 1060

Not certain but it might be worth having a look at this:

https://laravel.com/docs/5.1/eloquent-mutators#date-mutators

I'm pretty sure this will do the same sort of thing in Mongo and should play nice with blade templates

Upvotes: 2

Related Questions