Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates aggregate functions to return scalar values #15

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Contracts/ReviewRateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ public function ratings();

/**
*
* @return mix
* @return double
*/
public function averageRating($round= null);
public function averageRating($round = null);

/**
*
* @return mix
* @return int
*/
public function countRating();

/**
*
* @return mix
* @return double
*/
public function sumRating();

/**
* @param $max
*
* @return mix
* @return double
*/
public function ratingPercent($max = 5);

Expand Down
42 changes: 25 additions & 17 deletions src/Traits/ReviewRateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,60 @@ public function ratings()

/**
*
* @return mix
* @return double
*/
public function averageRating($round= null)
public function averageRating($round = null)
{
if ($round) {
return $this->ratings()
->selectRaw('ROUND(AVG(rating), '.$round.') as averageReviewRateable')
->pluck('averageReviewRateable');
$avgExpression = null;
if ($round) {
$avgExpression = 'ROUND(AVG(rating), ' . $round . ') as averageReviewRateable';
} else {
$avgExpression = 'AVG(rating) as averageReviewRateable';
}

return $this->ratings()
->selectRaw('AVG(rating) as averageReviewRateable')
->pluck('averageReviewRateable');
->selectRaw($avgExpression)
->get()
->first()
->averageReviewRateable;
}

/**
*
* @return mix
* @return int
*/
public function countRating(){
return $this->ratings()
->selectRaw('count(rating) as countReviewRateable')
->pluck('countReviewRateable');
public function countRating()
{
return $this->ratings()
->selectRaw('count(rating) as countReviewRateable')
->get()
->first()
->countReviewRateable;
}

/**
*
* @return mix
* @return double
*/
public function sumRating()
{
return $this->ratings()
->selectRaw('SUM(rating) as sumReviewRateable')
->pluck('sumReviewRateable');
->get()
->first()
->sumReviewRateable;
}

/**
* @param $max
*
* @return mix
* @return double
*/
public function ratingPercent($max = 5)
{
$ratings = $this->ratings();
$quantity = $ratings->count();
$total = $ratings->selectRaw('SUM(rating) as total')->pluck('total');
$total = $ratings->selectRaw('SUM(rating) as total')->get()->first()->total;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one there. I too came across this solve mine with
$total = $ratings->selectRaw('SUM(rating) as total')->pluck('total');
return ($quantity * $max) > 0 ? $total[0] / (($quantity * $max) / 100) : 0;

Copy link

@muarachmann muarachmann Jul 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Trexology thanks for this marvelous package. Can you please review this PR when you've got time :).

return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0;
}

Expand Down