Skip to content
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ $post->deleteRating(1);

### Fetch approved or not approved reviews/ratings for a particular resource
```php
// Get not approved ratings
// Get approved ratings
$ratings = $post->getApprovedRatings($post->id, 'desc');

// Get not approved ratings
Expand Down Expand Up @@ -140,6 +140,7 @@ or

````php
$post->averageRating(2) //round to 2 decimal place
$post->averageRating(null, true) //get only approved average rating
````

### Get all ratings:
Expand Down
68 changes: 50 additions & 18 deletions src/Traits/ReviewRateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,155 +17,187 @@ public function ratings()

/**
*
* @var $round
*
* @param $round
* @param $onlyApproved
* @return mixed
*/
public function averageRating($round= null)
public function averageRating($round= null, $onlyApproved= false)
{
$where = $onlyApproved ? [['approved', '1']] : [];

if ($round) {
return $this->ratings()
->selectRaw('ROUND(AVG(rating), '.$round.') as averageReviewRateable')
->where($where)
->pluck('averageReviewRateable');
}

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

/**
*
* @var $round
* @var $onlyApproved
* @return mixed
*/
public function averageCustomerServiceRating($round= null)
public function averageCustomerServiceRating($round= null, $onlyApproved= false)
{
$where = $onlyApproved ? [['approved', '1']] : [];

if ($round) {
return $this->ratings()
->selectRaw('ROUND(AVG(customer_service_rating), '.$round.') as averageCustomerServiceReviewRateable')
->where($where)
->pluck('averageCustomerServiceReviewRateable');
}

return $this->ratings()
->selectRaw('AVG(customer_service_rating) as averageCustomerServiceReviewRateable')
->where($where)
->pluck('averageCustomerServiceReviewRateable');
}

/**
*
* @param $round
* @param $onlyApproved
* @return mixed
*/
public function averageQualityRating($round = null)
public function averageQualityRating($round = null, $onlyApproved= false)
{
$where = $onlyApproved ? [['approved', '1']] : [];

if ($round) {
return $this->ratings()
->selectRaw('ROUND(AVG(quality_rating), '.$round.') as averageQualityReviewRateable')
->where($where)
->pluck('averageQualityReviewRateable');
}

return $this->ratings()
->selectRaw('AVG(quality_rating) as averageQualityReviewRateable')
->where($where)
->pluck('averageQualityReviewRateable');
}

/**
*
* @var $round
* @var $onlyApproved
* @return mixed
*/
public function averageFriendlyRating($round = null)
public function averageFriendlyRating($round = null, $onlyApproved= false)
{
$where = $onlyApproved ? [['approved', '1']] : [];

if ($round) {
return $this->ratings()
->selectRaw('ROUND(AVG(friendly_rating), '.$round.') as averageFriendlyReviewRateable')
->where($where)
->pluck('averageFriendlyReviewRateable');
}

return $this->ratings()
->selectRaw('AVG(friendly_rating) as averageFriendlyReviewRateable')
->where($where)
->pluck('averageFriendlyReviewRateable');
}

/**
*
* @var $round
* @var $onlyApproved
* @return mixed
*/
public function averagePricingRating($round = null)
public function averagePricingRating($round = null, $onlyApproved= false)
{
$where = $onlyApproved ? [['approved', '1']] : [];

if ($round) {
return $this->ratings()
->selectRaw('ROUND(AVG(pricing_rating), '.$round.') as averagePricingReviewRateable')
->where($where)
->pluck('averagePricingReviewRateable');
}

return $this->ratings()
->selectRaw('AVG(pricing_rating) as averagePricingReviewRateable')
->where($where)
->pluck('averagePricingReviewRateable');
}

/**
*
* @var $onlyApproved
* @return mixed
*/
public function countRating()
public function countRating($onlyApproved= false)
{
return $this->ratings()
->selectRaw('count(rating) as countReviewRateable')
->where($onlyApproved ? [['approved', '1']] : [])
->pluck('countReviewRateable');
}

/**
*
* @var $onlyApproved
* @return mixed
*/
public function countCustomerServiceRating()
public function countCustomerServiceRating($onlyApproved= false)
{
return $this->ratings()
->selectRaw('count(customer_service_rating) as countCustomerServiceReviewRateable')
->where($onlyApproved ? [['approved', '1']] : [])
->pluck('countCustomerServiceReviewRateable');
}

/**
*
* @var $onlyApproved
* @return mixed
*/
public function countQualityRating()
public function countQualityRating($onlyApproved= false)
{
return $this->ratings()
->selectRaw('count(quality_rating) as countQualityReviewRateable')
->where($onlyApproved ? [['approved', '1']] : [])
->pluck('countQualityReviewRateable');
}

/**
*
* @var $onlyApproved
* @return mixed
*/
public function countFriendlyRating() {
public function countFriendlyRating($onlyApproved= false) {
return $this->ratings()
->selectRaw('count(friendly_rating) as countFriendlyReviewRateable')
->where($onlyApproved ? [['approved', '1']] : [])
->pluck('countFriendlyReviewRateable');
}

/**
*
* @var $onlyApproved
* @return mixed
*/
public function countPriceRating() {
public function countPriceRating($onlyApproved= false) {
return $this->ratings()
->selectRaw('count(price_rating) as countPriceReviewRateable')
->where($onlyApproved ? [['approved', '1']] : [])
->pluck('countPriceReviewRateable');
}

/**
*
* @var $onlyApproved
* @return mixed
*/
public function sumRating()
public function sumRating($onlyApproved= false)
{
return $this->ratings()
->selectRaw('SUM(rating) as sumReviewRateable')
->where($onlyApproved ? [['approved', '1']] : [])
->pluck('sumReviewRateable');
}

Expand Down