Skip to content

Commit 5fad4e5

Browse files
authored
Merge pull request #12 from Bessamu/only-approved
Add only approved average and readme update
2 parents 36935b6 + 742878a commit 5fad4e5

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ $post->deleteRating(1);
9999

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

105105
// Get not approved ratings
@@ -140,6 +140,7 @@ or
140140

141141
````php
142142
$post->averageRating(2) //round to 2 decimal place
143+
$post->averageRating(null, true) //get only approved average rating
143144
````
144145

145146
### Get all ratings:

src/Traits/ReviewRateable.php

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,155 +17,187 @@ public function ratings()
1717

1818
/**
1919
*
20-
* @var $round
20+
*
21+
* @param $round
22+
* @param $onlyApproved
2123
* @return mixed
2224
*/
23-
public function averageRating($round= null)
25+
public function averageRating($round= null, $onlyApproved= false)
2426
{
27+
$where = $onlyApproved ? [['approved', '1']] : [];
28+
2529
if ($round) {
2630
return $this->ratings()
2731
->selectRaw('ROUND(AVG(rating), '.$round.') as averageReviewRateable')
32+
->where($where)
2833
->pluck('averageReviewRateable');
2934
}
3035

3136
return $this->ratings()
3237
->selectRaw('AVG(rating) as averageReviewRateable')
38+
->where($where)
3339
->pluck('averageReviewRateable');
3440
}
3541

3642
/**
3743
*
3844
* @var $round
45+
* @var $onlyApproved
3946
* @return mixed
4047
*/
41-
public function averageCustomerServiceRating($round= null)
48+
public function averageCustomerServiceRating($round= null, $onlyApproved= false)
4249
{
50+
$where = $onlyApproved ? [['approved', '1']] : [];
51+
4352
if ($round) {
4453
return $this->ratings()
4554
->selectRaw('ROUND(AVG(customer_service_rating), '.$round.') as averageCustomerServiceReviewRateable')
55+
->where($where)
4656
->pluck('averageCustomerServiceReviewRateable');
4757
}
4858

4959
return $this->ratings()
5060
->selectRaw('AVG(customer_service_rating) as averageCustomerServiceReviewRateable')
61+
->where($where)
5162
->pluck('averageCustomerServiceReviewRateable');
5263
}
5364

5465
/**
5566
*
5667
* @param $round
68+
* @param $onlyApproved
5769
* @return mixed
5870
*/
59-
public function averageQualityRating($round = null)
71+
public function averageQualityRating($round = null, $onlyApproved= false)
6072
{
73+
$where = $onlyApproved ? [['approved', '1']] : [];
74+
6175
if ($round) {
6276
return $this->ratings()
6377
->selectRaw('ROUND(AVG(quality_rating), '.$round.') as averageQualityReviewRateable')
78+
->where($where)
6479
->pluck('averageQualityReviewRateable');
6580
}
6681

6782
return $this->ratings()
6883
->selectRaw('AVG(quality_rating) as averageQualityReviewRateable')
84+
->where($where)
6985
->pluck('averageQualityReviewRateable');
7086
}
7187

7288
/**
7389
*
7490
* @var $round
91+
* @var $onlyApproved
7592
* @return mixed
7693
*/
77-
public function averageFriendlyRating($round = null)
94+
public function averageFriendlyRating($round = null, $onlyApproved= false)
7895
{
96+
$where = $onlyApproved ? [['approved', '1']] : [];
97+
7998
if ($round) {
8099
return $this->ratings()
81100
->selectRaw('ROUND(AVG(friendly_rating), '.$round.') as averageFriendlyReviewRateable')
101+
->where($where)
82102
->pluck('averageFriendlyReviewRateable');
83103
}
84104

85105
return $this->ratings()
86106
->selectRaw('AVG(friendly_rating) as averageFriendlyReviewRateable')
107+
->where($where)
87108
->pluck('averageFriendlyReviewRateable');
88109
}
89110

90111
/**
91112
*
92113
* @var $round
114+
* @var $onlyApproved
93115
* @return mixed
94116
*/
95-
public function averagePricingRating($round = null)
117+
public function averagePricingRating($round = null, $onlyApproved= false)
96118
{
119+
$where = $onlyApproved ? [['approved', '1']] : [];
120+
97121
if ($round) {
98122
return $this->ratings()
99123
->selectRaw('ROUND(AVG(pricing_rating), '.$round.') as averagePricingReviewRateable')
124+
->where($where)
100125
->pluck('averagePricingReviewRateable');
101126
}
102127

103128
return $this->ratings()
104129
->selectRaw('AVG(pricing_rating) as averagePricingReviewRateable')
130+
->where($where)
105131
->pluck('averagePricingReviewRateable');
106132
}
107133

108134
/**
109-
*
135+
* @var $onlyApproved
110136
* @return mixed
111137
*/
112-
public function countRating()
138+
public function countRating($onlyApproved= false)
113139
{
114140
return $this->ratings()
115141
->selectRaw('count(rating) as countReviewRateable')
142+
->where($onlyApproved ? [['approved', '1']] : [])
116143
->pluck('countReviewRateable');
117144
}
118145

119146
/**
120-
*
147+
* @var $onlyApproved
121148
* @return mixed
122149
*/
123-
public function countCustomerServiceRating()
150+
public function countCustomerServiceRating($onlyApproved= false)
124151
{
125152
return $this->ratings()
126153
->selectRaw('count(customer_service_rating) as countCustomerServiceReviewRateable')
154+
->where($onlyApproved ? [['approved', '1']] : [])
127155
->pluck('countCustomerServiceReviewRateable');
128156
}
129157

130158
/**
131-
*
159+
* @var $onlyApproved
132160
* @return mixed
133161
*/
134-
public function countQualityRating()
162+
public function countQualityRating($onlyApproved= false)
135163
{
136164
return $this->ratings()
137165
->selectRaw('count(quality_rating) as countQualityReviewRateable')
166+
->where($onlyApproved ? [['approved', '1']] : [])
138167
->pluck('countQualityReviewRateable');
139168
}
140169

141170
/**
142-
*
171+
* @var $onlyApproved
143172
* @return mixed
144173
*/
145-
public function countFriendlyRating() {
174+
public function countFriendlyRating($onlyApproved= false) {
146175
return $this->ratings()
147176
->selectRaw('count(friendly_rating) as countFriendlyReviewRateable')
177+
->where($onlyApproved ? [['approved', '1']] : [])
148178
->pluck('countFriendlyReviewRateable');
149179
}
150180

151181
/**
152-
*
182+
* @var $onlyApproved
153183
* @return mixed
154184
*/
155-
public function countPriceRating() {
185+
public function countPriceRating($onlyApproved= false) {
156186
return $this->ratings()
157187
->selectRaw('count(price_rating) as countPriceReviewRateable')
188+
->where($onlyApproved ? [['approved', '1']] : [])
158189
->pluck('countPriceReviewRateable');
159190
}
160191

161192
/**
162-
*
193+
* @var $onlyApproved
163194
* @return mixed
164195
*/
165-
public function sumRating()
196+
public function sumRating($onlyApproved= false)
166197
{
167198
return $this->ratings()
168199
->selectRaw('SUM(rating) as sumReviewRateable')
200+
->where($onlyApproved ? [['approved', '1']] : [])
169201
->pluck('sumReviewRateable');
170202
}
171203

0 commit comments

Comments
 (0)