laravel 5.1 mongodb(jenssegers / laravel-mongodb)查询日期不能正常工作

问题描述 投票:0回答:1

我正在使用laravel 5.1和mongodb包(jenssegers / laravel-mongodb)来构建一个潜在客户经理系统。我试图从威慑时间获得潜在客户的数量,但回答的答案是错误的 - 例如,如果对于$today_count我保持与总数相同(我知道错了,我可以通过集合中的日期看到)我的代码看起来像这样:

public function getCount()
{
    $client_id = \Auth::client()->id;
    $today = Carbon::now()->startOfDay();
    $today->setTimezone(Client::getTimezone())->toDateTimeString();
    $last_week = $today->subWeek();
    $today_count = Lead::where('client_id',$client_id)->where('created_at','>',$today)->get()->count();
    $last_week_count = Lead::where('created_at',$last_week)->where('client_id',$client_id)->get()->count();
    $forever = Lead::where('client_id',$client_id)->get()->count();
    return ['today' => $today_count, 'last_week' => $last_week_count, 'forever' => $forever];
}

我该怎么做才能得到正确的答案?

php mongodb laravel-5.1
1个回答
0
投票

解决了!

我发现问题是什么,MongoDB日期格式与MySQL不同,在查询MongoDB中的日期之前,你通过PHP对象调用MongoDate转换为mongo date formt:http://php.net/manual/en/class.mongodate.php

例如$today应该是这样的:

$today = new \MongoDate(Carbon::now()->startOfDay()->timestamp);
© www.soinside.com 2019 - 2024. All rights reserved.