收集我有下面這段代碼讓所有的評級信息的模型,你可以看到有相當多的查詢事情只是評級信息的可在此功能可以更好地尤其是做對於查詢有效負載,每次請求這些信息時都不會耗盡服務器。laravel查詢這就是被稱爲pior
public function ratingInfo() {
$totalCount = $this->ratings()->count();
$one_star = $this->ratings()->where('rating', '>', 0)->where('ratings', '<=', 1);
$two_star = $this->ratings()->where('rating', '>', 1)->where('ratings', '<=', 2);
$three_star = $this->ratings()->where('rating', '>', 2)->where('ratings', '<=', 3);
$four_star = $this->ratings()->where('rating', '>', 3)->where('ratings', '<=', 4);
$three_star = $this->ratings()->where('rating', '>', 4)->where('ratings', '<=', 5);
return [
'avgRating' => $this->avgRating(),
'ratingCount' => $this->ratingCount(),
'one_star' => ['count' => $one_star->count(), 'percent' => round(($one_star->count() * $totalCount)/100, 2)],
'two_star' => ['count' => $two_star->count(), 'percent' => round(($two_star->count() * $totalCount)/100, 2)],
'three_star' => ['count' => $two_star->count(), 'percent' => round(($three_star->count() * $totalCount)/100, 2)],
'four_star' => ['count' => $four_star->count(), 'percent' => round(($four_star->count() * $totalCount)/100, 2)],
'five_star' => ['count' => $five_star->count(), 'percent' => round(($five_star->count() * $totalCount)/100, 2)],
];
}
我是這樣想的,如果你只有一個查詢就像這樣開始。
$ratings = $this->ratings->all();
/* then use $ratings again and again how*/
$totalCount = $ratings->count();
$one_star = then query the $ratings collection here but how
評級型號:
class Rating extends Model
{
protected $table = 'ratings';
protected $fillable = ['rating', 'comment', 'user_id', 'rateable_id', 'rateable_type'];
public function rating()
{
return $this->morphTo();
}
}
這就是答案:
public function ratingInfo() {
$result = [];
$one_star = $this->ratings->filter(function ($item, $key) {
return $item->rating > 0 && $item->rating <= 1;
});
$two_star = $this->ratings->filter(function ($item, $key) {
return $item->rating > 1 && $item->rating <= 2;
});
$three_star = $this->ratings->filter(function ($item, $key) {
return $item->rating > 2 && $item->rating <= 3;
});
$four_star = $this->ratings->filter(function ($item, $key) {
return $item->rating > 3 && $item->rating <= 4;
});
$five_star = $this->ratings->filter(function ($item, $key) {
return $item->rating > 4 && $item->rating <= 5;
});
$totalCount = $this->ratings->count();
$avgRating = $this->avgRating(2);
$totalRatings = $this->ratings->sum('rating');
//dd('sum: ' . $one_star->sum('rating') . ' count: ' . $one_star->count() . ' percent: ' . round(($one_star->sum('rating')/$this->ratings->sum('rating')) * 100, 2));
return [
'total_count' => $totalCount,
'average_rating' => $avgRating,
'total_ratings' => $totalRatings,
'one_star' => [
'sum' => $one_star->sum('rating'),
'count' => $one_star->count(),
'percent' => round(($one_star->sum('rating')/$totalRatings) * 100, 2)
],
'two_star' => [
'sum' => $two_star->sum('rating'),
'count' => $two_star->count(),
'percent' => round(($two_star->sum('rating')/$totalRatings) * 100, 2)
],
'three_star' => [
'sum' => $three_star->sum('rating'),
'count' => $three_star->count(),
'percent' => round(($three_star->sum('rating')/$totalRatings) * 100, 2)
],
'four_star' => [
'sum' => $four_star->sum('rating'),
'count' => $four_star->count(),
'percent' => round(($four_star->sum('rating')/$totalRatings) * 100, 2)
],
'five_star' => [
'sum' => $five_star->sum('rating'),
'count' => $five_star->count(),
'percent' => round(($five_star->sum('rating')/$totalRatings) * 100, 2)
]
];
}
我增加了一個possibilty,此時用只是收集操作的收視率 – btl