Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 按平均评分搜索行_Laravel_Laravel 5.4 - Fatal编程技术网

Laravel 按平均评分搜索行

Laravel 按平均评分搜索行,laravel,laravel-5.4,Laravel,Laravel 5.4,将有用户可以给的职位评级,我如何才能搜索其平均评级的职位 后模型 //post model public function avgRating() { return $this->ratings()->selectRaw('avg(ratings) as avgRatings,post_id')->groupBy('post_id'); } public function getAvgRatingAttribute() { if ( ! array_key_

将有用户可以给的职位评级,我如何才能搜索其平均评级的职位

后模型

//post model

public function avgRating()
{
    return $this->ratings()->selectRaw('avg(ratings) as avgRatings,post_id')->groupBy('post_id');
}

public function getAvgRatingAttribute()
{
    if ( ! array_key_exists('avgRating', $this->relations)) {
       $this->load('avgRating');
    }
    $relation = $this->getRelation('avgRating')->first();
    return ($relation) ? $relation->avgRatings : null;
}
通过上面的代码,我可以获得每个帖子的平均评分,现在我如何通过评分筛选帖子,比如获得平均评分1或平均评分2或平均评分3或平均评分4或平均评分5

 //To filter data
 public function filter()
 {
     $post= (new Post)->newQuery();

     if ($request->has('averageRatings')) {
        $post->whereHas('avgRating', function ($query) use ($request) {
        $query->where('rating', $request->input('averageRatings'))->avgRating;
        });
    }
   $results = $institute->get();
 }

使用where()子句,如where('ratings.ratings'、['1'、'2'、'3'、'4'、'5']);ratings.ratings与您在select中所称的列不匹配。。此外,您不应该精确地比较它,您不应该使用大于或小于?不,这不起作用。@sunitiyadavuse where()子句,如where('ratings.ratings'、['1'、'2'、'3'、'4'、'5']);ratings.ratings与您在select中所称的列不匹配。。此外,您不应该精确地比较它,您不应该使用大于或小于?不,这不起作用。@sunitiyadav