doqs8936 2015-10-27 09:09
浏览 251
已采纳

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

I'm using laravel 5.1 with mongodb package (jenssegers/laravel-mongodb) to build a leads manager system. I'm trying to get the count of leads from deterrent times but the return answer is wrong - for example if for $today_count I keep get the same as total count (I know it wrong, I can see by the dates in the collection) my code looks like that:

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];
}

What should I do to get the correct answer?

  • 写回答

1条回答 默认 最新

  • doumu5662 2015-11-04 11:33
    关注

    Solved!

    i found what the problem was, MongoDB date format is different then MySQL, before querying dates in MongoDB you shold convert in to mongo date formt via PHP object call MongoDate:http://php.net/manual/en/class.mongodate.php

    for example $today should look like this:

    $today = new \MongoDate(Carbon::now()->startOfDay()->timestamp);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)