Eloquent WHERE不会返回预期结果

时间:2016-02-24 15:11:39

标签: php laravel eloquent

我希望下面的代码可以在每个33.3%旁边的评论中查找 :

$ratings->where

数据库中只有一个评分为-1,其他值的计数结果为0。但是我在所有情况下都得到0。

让我思考的是,如果我改变第一个“喜欢”,其中-1而不是1,我得到的结果为1喜欢和“不喜欢”但是对于“not_sure”它仍为0。 / p>

我无法理解为什么会这样。

修改

数据库只包含一条记录

$ratings = VideoRating::where('video_id', '=', $video->id) ->where('created_at','>=', \Carbon\Carbon::createFromFormat('Y-m-d',$start_date))->where('created_at', '<=', \Carbon\Carbon::createFromFormat('Y-m-d',$end_date)); $data = [ "likes" => $ratings->where('rating','=',1)->get()->count(), //0 "dislikes" => $ratings->where('rating','=',-1)->get()->count(), //1 "not_sure" => $ratings->where('rating','=',0)->get()->count(), // 0 ];

我使用的日期为INSERT INTO "public"."video_ratings(id,user_id,video_id,rating,created_at, updated_at)" VALUES ('1', '2', '1', '-1', '2016-02-24 14:57:41', '2016-02-24 14:57:41');01/02/2016,因此它在created_at期间内

编辑2

肯定它与变异29/02/2016变量有关。虽然我并不精通Eloquent以确切知道如何,但下面的代码按预期工作

$ratings

我不喜欢它,我宁愿以适当的方式重用初始对象 $ratings = VideoRating::where('video_id', '=', $video->id) ->where('created_at', '>=', \Carbon\Carbon::createFromFormat('Y-m-d', $start_date)->toDateTimeString())->where('created_at', '<=', \Carbon\Carbon::createFromFormat('Y-m-d', $end_date)->toDateTimeString()); $ratings1 = VideoRating::where('video_id', '=', $video->id) ->where('created_at', '>=', \Carbon\Carbon::createFromFormat('Y-m-d', $start_date)->toDateTimeString())->where('created_at', '<=', \Carbon\Carbon::createFromFormat('Y-m-d', $end_date)->toDateTimeString()); $ratings2 = VideoRating::where('video_id', '=', $video->id) ->where('created_at', '>=', \Carbon\Carbon::createFromFormat('Y-m-d', $start_date)->toDateTimeString())->where('created_at', '<=', \Carbon\Carbon::createFromFormat('Y-m-d', $end_date)->toDateTimeString()); $data = [ "likes" => $ratings->where('rating', '=', 1)->get()->count(), "dislikes" => $ratings1->where('rating', '=', -1)->get()->count(), "not_sure" => $ratings2->where('rating', '=', 0)->get()->count(), ];

2 个答案:

答案 0 :(得分:1)

如果您需要来自同一基本查询的不同结果,则需要在执行get / count方法之前克隆它:

$likes = clone $ratings;
$dislikes = clone $ratings;
$not_sure = clone $ratings;

$data = [
    "likes"     => $likes->where('rating', 1)->count(),
    "dislikes"  => $dislikes->where('rating', -1)->count(),
    "not_sure"  => $not_sure->where('rating', 0)->count(),
];

答案 1 :(得分:1)

由于PHP中实现对象的方式,可能会出现您的问题

尝试使用Eloquent中的查询范围:

  1. 修改您的VideoRating课程并添加以下方法
  2. public function scopeFetchRatingsBetweenDatesForVideo($query, $videoID, $startDate, $endDate, $rating = null){
        $query->whereVideoId(videoID)
            ->whereRaw('DATE_FORMAT(`created_at`, "%Y-%m-%d") >= ?', [$startDate])
            ->whereRaw('DATE_FORMAT(`created_at`, "%Y-%m-%d") <= ?', [$endDate])
    
        if($rating != null){
            $query->whereRating($rating)
        }
        return $query
    }   
    
    1. 使用您的范围
    2. 如果您的输入开始日期和结束日期的格式为Y-md-d,则不应将它们翻译为Y-m-d H:i:s。一个选项是将它们用作Y-m-d并将created_at字段转换为Y-m-d。如果您的输入日期是Y-m-d H:i:我可以将输入日期与created_at字段进行比较。

       $startDate = \Carbon\Carbon::createFromFormat('Y-m-d',$start_date)->toDateString();
       $endDate = \Carbon\Carbon::createFromFormat('Y-m-d', $end_date)->toDateString();
       $data = [
          'likes' => VideoRating::fetchRatingsBetweenDatesForVideo($video->id, $startDate, $endDate, 1)->count(),
          'dislikes' => VideoRating::fetchRatingsBetweenDatesForVideo($video->id, $startDate, $endDate, -1)->count(),
          'not_sure' => VideoRating::fetchRatingsBetweenDatesForVideo($video->id, $startDate, $endDate, 0)->count(),
      ];
      
      // or
      $ratings = VideoRating::fetchRatingsBetweenDatesForVideo($video->id, $startDate, $endDate);
      $data = [
          'likes' => (clone $ratings)->whereRating(1)->count(),
          'dislikes' => (clone $ratings)->whereRating(-1)->count(),
          'not_sure' => (clone $ratings)->whereRating(0)->count(),
      ];