我正在使用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];
}
我该怎么做才能得到正确的答案?
我发现问题是什么,MongoDB日期格式与MySQL不同,在查询MongoDB中的日期之前,你通过PHP对象调用MongoDate转换为mongo date formt:http://php.net/manual/en/class.mongodate.php
例如$today
应该是这样的:
$today = new \MongoDate(Carbon::now()->startOfDay()->timestamp);