Skip to content

Commit bb8b4fc

Browse files
committed
Working on selecting reviews with average ratings
1 parent 3286cc5 commit bb8b4fc

File tree

3 files changed

+84
-24
lines changed

3 files changed

+84
-24
lines changed

src/Contracts/ReviewRateable.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function sumRating();
8585
/**
8686
*
8787
* @param $max
88+
*
8889
* @return mixed
8990
*/
9091
public function ratingPercent($max = 5);
@@ -94,6 +95,7 @@ public function ratingPercent($max = 5);
9495
* @param $data
9596
* @param $author
9697
* @param $parent
98+
*
9799
* @return mixed
98100
*/
99101
public function rating($data, Model $author, Model $parent = null);
@@ -103,6 +105,7 @@ public function rating($data, Model $author, Model $parent = null);
103105
* @param $id
104106
* @param $data
105107
* @param $parent
108+
*
106109
* @return mixed
107110
*/
108111
public function updateRating($id, $data, Model $parent = null);
@@ -111,6 +114,7 @@ public function updateRating($id, $data, Model $parent = null);
111114
*
112115
* @param $id
113116
* @param $sort
117+
*
114118
* @return mixed
115119
*/
116120
public function getAllRatings($id, $sort = 'desc');
@@ -119,6 +123,7 @@ public function getAllRatings($id, $sort = 'desc');
119123
*
120124
* @param $id
121125
* @param $sort
126+
*
122127
* @return mixed
123128
*/
124129
public function getApprovedRatings($id, $sort = 'desc');
@@ -127,6 +132,7 @@ public function getApprovedRatings($id, $sort = 'desc');
127132
*
128133
* @param $id
129134
* @param $sort
135+
*
130136
* @return mixed
131137
*/
132138
public function getNotApprovedRatings($id, $sort = 'desc');
@@ -135,6 +141,7 @@ public function getNotApprovedRatings($id, $sort = 'desc');
135141
* @param $id
136142
* @param $limit
137143
* @param $sort
144+
*
138145
* @return mixed
139146
*/
140147
public function getRecentRatings($id, $limit = 5, $sort = 'desc');
@@ -144,15 +151,25 @@ public function getRecentRatings($id, $limit = 5, $sort = 'desc');
144151
* @param $limit
145152
* @param $approved
146153
* @param $sort
154+
*
147155
* @return mixed
148156
*/
149157
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc');
150158

151-
public function getCollectionByAverageRating($rating);
159+
/**
160+
* @param $rating
161+
* @para $type
162+
* @param $approved
163+
* @param $sort
164+
*
165+
* @return mixed
166+
*/
167+
public function getCollectionByAverageRating($rating, $type = 'rating', $approved = true, $sort = 'desc');
152168

153169
/**
154170
*
155171
* @param $id
172+
*
156173
* @return mixed
157174
*/
158175
public function deleteRating($id);

src/Models/Rating.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Codebyray\ReviewRateable\Models;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Rating extends Model
@@ -11,6 +12,16 @@ class Rating extends Model
1112
*/
1213
protected $table = 'reviews';
1314

15+
/**
16+
* @var string
17+
*/
18+
protected $rating;
19+
20+
/**
21+
* @var string
22+
*/
23+
protected $type;
24+
1425
/**
1526
* @var array
1627
*/
@@ -69,6 +80,7 @@ public function updateRating($id, $data)
6980
/**
7081
* @param $id
7182
* @param $sort
83+
*
7284
* @return mixed
7385
*/
7486
public function getAllRatings($id, $sort = 'desc')
@@ -84,6 +96,7 @@ public function getAllRatings($id, $sort = 'desc')
8496
/**
8597
* @param $id
8698
* @param $sort
99+
*
87100
* @return mixed
88101
*/
89102
public function getApprovedRatings($id, $sort = 'desc')
@@ -100,6 +113,7 @@ public function getApprovedRatings($id, $sort = 'desc')
100113
/**
101114
* @param $id
102115
* @param $sort
116+
*
103117
* @return mixed
104118
*/
105119
public function getNotApprovedRatings($id, $sort = 'desc')
@@ -117,6 +131,7 @@ public function getNotApprovedRatings($id, $sort = 'desc')
117131
* @param $id
118132
* @param $limit
119133
* @param $sort
134+
*
120135
* @return mixed
121136
*/
122137
public function getRecentRatings($id, $limit = 5, $sort = 'desc')
@@ -136,6 +151,7 @@ public function getRecentRatings($id, $limit = 5, $sort = 'desc')
136151
* @param $limit
137152
* @param $approved
138153
* @param $sort
154+
*
139155
* @return mixed
140156
*/
141157
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc')
@@ -150,23 +166,26 @@ public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort =
150166
return $rating;
151167
}
152168

153-
public function getCollectionByAverageRating($rating)
169+
/**
170+
* @param $rating
171+
* @param $type
172+
* @param $approved
173+
* @param $sort
174+
*
175+
* @return mixed
176+
*/
177+
public function getCollectionByAverageRating($rating, $type = 'rating', $approved = true, $sort = 'asc')
154178
{
155-
$ratings = $this->selectRaw('reviewrateable_id as modelId, AVG(rating) as avgRating')
156-
->with($this->reviewrateable())
157-
->groupBy('modelId')
158-
->havingRaw('AVG(rating) >= '.$rating)
159-
->get();
160-
161-
$ratings = $ratings->mapToGroups(function ($item, $key) {
162-
return [$item['name'] => $item['reviewrateable']];
163-
});
179+
$this->rating = $rating;
180+
$this->type = $type;
164181

165-
// Do something to separate the featurable models in each subsection by model type
166-
foreach($ratings as $i => $rows) {
167-
$ratings[$i] = $rows->groupBy('reviewrateable_type');
168-
}
182+
$ratings = $this->whereHasMorph('reviewrateable', '*', function (Builder $query) {
183+
return $query->groupBy('reviewrateable_id')
184+
->havingRaw('AVG('.$this->type.') >= '.$this->rating);
185+
})->where('approved', $approved)
186+
->orderBy($type, $sort)->get();
169187

188+
// ddd($ratings);
170189
return $ratings;
171190
}
172191

src/Traits/ReviewRateable.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function ratings()
2020
*
2121
* @param $round
2222
* @param $onlyApproved
23+
*
2324
* @return mixed
2425
*/
2526
public function averageRating($round= null, $onlyApproved= false)
@@ -43,6 +44,7 @@ public function averageRating($round= null, $onlyApproved= false)
4344
*
4445
* @var $round
4546
* @var $onlyApproved
47+
*
4648
* @return mixed
4749
*/
4850
public function averageCustomerServiceRating($round= null, $onlyApproved= false)
@@ -66,6 +68,7 @@ public function averageCustomerServiceRating($round= null, $onlyApproved= false)
6668
*
6769
* @param $round
6870
* @param $onlyApproved
71+
*
6972
* @return mixed
7073
*/
7174
public function averageQualityRating($round = null, $onlyApproved= false)
@@ -89,6 +92,7 @@ public function averageQualityRating($round = null, $onlyApproved= false)
8992
*
9093
* @var $round
9194
* @var $onlyApproved
95+
*
9296
* @return mixed
9397
*/
9498
public function averageFriendlyRating($round = null, $onlyApproved= false)
@@ -112,9 +116,10 @@ public function averageFriendlyRating($round = null, $onlyApproved= false)
112116
*
113117
* @var $round
114118
* @var $onlyApproved
119+
*
115120
* @return mixed
116121
*/
117-
public function averagePricingRating($round = null, $onlyApproved= false)
122+
public function averagePricingRating($round = null, $onlyApproved = false)
118123
{
119124
$where = $onlyApproved ? [['approved', '1']] : [];
120125

@@ -133,9 +138,10 @@ public function averagePricingRating($round = null, $onlyApproved= false)
133138

134139
/**
135140
* @var $onlyApproved
141+
*
136142
* @return mixed
137143
*/
138-
public function countRating($onlyApproved= false)
144+
public function countRating($onlyApproved = false)
139145
{
140146
return $this->ratings()
141147
->selectRaw('count(rating) as countReviewRateable')
@@ -145,9 +151,10 @@ public function countRating($onlyApproved= false)
145151

146152
/**
147153
* @var $onlyApproved
154+
*
148155
* @return mixed
149156
*/
150-
public function countCustomerServiceRating($onlyApproved= false)
157+
public function countCustomerServiceRating($onlyApproved = false)
151158
{
152159
return $this->ratings()
153160
->selectRaw('count(customer_service_rating) as countCustomerServiceReviewRateable')
@@ -157,9 +164,10 @@ public function countCustomerServiceRating($onlyApproved= false)
157164

158165
/**
159166
* @var $onlyApproved
167+
*
160168
* @return mixed
161169
*/
162-
public function countQualityRating($onlyApproved= false)
170+
public function countQualityRating($onlyApproved = false)
163171
{
164172
return $this->ratings()
165173
->selectRaw('count(quality_rating) as countQualityReviewRateable')
@@ -169,9 +177,10 @@ public function countQualityRating($onlyApproved= false)
169177

170178
/**
171179
* @var $onlyApproved
180+
*
172181
* @return mixed
173182
*/
174-
public function countFriendlyRating($onlyApproved= false) {
183+
public function countFriendlyRating($onlyApproved = false) {
175184
return $this->ratings()
176185
->selectRaw('count(friendly_rating) as countFriendlyReviewRateable')
177186
->where($onlyApproved ? [['approved', '1']] : [])
@@ -180,9 +189,10 @@ public function countFriendlyRating($onlyApproved= false) {
180189

181190
/**
182191
* @var $onlyApproved
192+
*
183193
* @return mixed
184194
*/
185-
public function countPriceRating($onlyApproved= false) {
195+
public function countPriceRating($onlyApproved = false) {
186196
return $this->ratings()
187197
->selectRaw('count(price_rating) as countPriceReviewRateable')
188198
->where($onlyApproved ? [['approved', '1']] : [])
@@ -191,9 +201,10 @@ public function countPriceRating($onlyApproved= false) {
191201

192202
/**
193203
* @var $onlyApproved
204+
*
194205
* @return mixed
195206
*/
196-
public function sumRating($onlyApproved= false)
207+
public function sumRating($onlyApproved = false)
197208
{
198209
return $this->ratings()
199210
->selectRaw('SUM(rating) as sumReviewRateable')
@@ -242,6 +253,7 @@ public function updateRating($id, $data, Model $parent = null)
242253
*
243254
* @param $id
244255
* @param $sort
256+
*
245257
* @return mixed
246258
*/
247259
public function getAllRatings($id, $sort = 'desc')
@@ -253,6 +265,7 @@ public function getAllRatings($id, $sort = 'desc')
253265
*
254266
* @param $id
255267
* @param $sort
268+
*
256269
* @return mixed
257270
*/
258271
public function getApprovedRatings($id, $sort = 'desc')
@@ -264,6 +277,7 @@ public function getApprovedRatings($id, $sort = 'desc')
264277
*
265278
* @param $id
266279
* @param $sort
280+
*
267281
* @return mixed
268282
*/
269283
public function getNotApprovedRatings($id, $sort = 'desc')
@@ -275,6 +289,7 @@ public function getNotApprovedRatings($id, $sort = 'desc')
275289
* @param $id
276290
* @param $limit
277291
* @param $sort
292+
*
278293
* @return mixed
279294
*/
280295
public function getRecentRatings($id, $limit = 5, $sort = 'desc')
@@ -287,16 +302,25 @@ public function getRecentRatings($id, $limit = 5, $sort = 'desc')
287302
* @param $limit
288303
* @param $approved
289304
* @param $sort
305+
*
290306
* @return mixed
291307
*/
292308
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc')
293309
{
294310
return (new Rating())->getRecentUserRatings($id, $limit, $approved, $sort);
295311
}
296312

297-
public function getCollectionByAverageRating($rating)
313+
/**
314+
* @param $rating
315+
* @param $type
316+
* @param $approved
317+
* @param $sort
318+
*
319+
* @return mixed
320+
*/
321+
public function getCollectionByAverageRating($rating, $type = 'rating', $approved = true, $sort = 'desc')
298322
{
299-
return (new Rating())->getCollectionByAverageRating($rating);
323+
return (new Rating())->getCollectionByAverageRating($rating, $approved, $sort);
300324
}
301325

302326
/**

0 commit comments

Comments
 (0)