Skip to content

Commit 1727d00

Browse files
committed
Added getReviewsByRating.
1 parent 8a158c8 commit 1727d00

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Contracts/ReviewRateableContract.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,15 @@ public function ratingCounts(?string $department = null, bool $approved = true):
150150
* @return array
151151
*/
152152
public function ratingStats(?string $department = null, bool $approved = true): array;
153+
154+
/**
155+
* Return reviews based on star ratings.
156+
*
157+
* @param int $starValue
158+
* @param string|null $department
159+
* @param bool $approved
160+
* @param bool $withRatings
161+
* @return Collection
162+
*/
163+
public function getReviewsByRating(int $starValue, string $department = null, bool $approved = true, bool $withRatings = true): Collection;
153164
}

src/Services/ReviewRateableService.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,19 @@ public function ratingStats(?string $department = null, bool $approved = true):
236236
{
237237
return $this->getModel()->ratingStats($department, $approved);
238238
}
239+
240+
/**
241+
* Return reviews based on star ratings.
242+
*
243+
* @param int $starValue
244+
* @param string|null $department
245+
* @param bool $approved
246+
* @param bool $withRatings
247+
* @return Collection
248+
* @throws Exception
249+
*/
250+
public function getReviewsByRating(int $starValue, string $department = null, bool $approved = true, bool $withRatings = true): Collection
251+
{
252+
return $this->getModel()->getReviewsByRating($starValue, $department, $approved, $withRatings);
253+
}
239254
}

src/Traits/ReviewRateable.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,29 @@ public function ratingStats(?string $department = null, bool $approved = true):
472472
'total' => $total,
473473
];
474474
}
475+
476+
/**
477+
* Return reviews based on star ratings.
478+
*
479+
* @param int $starValue
480+
* @param string|null $department
481+
* @param bool $approved
482+
* @param bool $withRatings
483+
* @return Collection
484+
*/
485+
public function getReviewsByRating(int $starValue, string $department = null, bool $approved = true, bool $withRatings = true): Collection
486+
{
487+
$query = $this->reviews()
488+
->when($approved, fn($q) => $q->where('approved', $approved))
489+
->when($department, fn($q) => $q->where('department', $department))
490+
->whereHas('ratings', fn($q) =>
491+
$q->where('value', $starValue)
492+
);
493+
494+
if ($withRatings) {
495+
$query->with('ratings');
496+
}
497+
498+
return $query->get();
499+
}
475500
}

0 commit comments

Comments
 (0)