Skip to content

Commit 586c818

Browse files
committed
fix: update latest received reviews query for flexibility
1 parent 3cd4d3b commit 586c818

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/Traits/CanBeReviewed.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Fajarwz\LaravelReview\Models\Review;
66
use Fajarwz\LaravelReview\Models\ReviewSummary;
7-
use Illuminate\Database\Eloquent\Collection;
87
use Illuminate\Database\Eloquent\Model;
98
use Illuminate\Database\Eloquent\Relations\HasMany;
109
use Illuminate\Database\Eloquent\Relations\MorphOne;
@@ -63,17 +62,17 @@ public function getReceivedReview(Model $model, bool $includeUnapproved = false)
6362
}
6463

6564
/**
66-
* Get the current model latest received reviews of the specified model.
65+
* Query the current model latest received reviews of the specified model.
6766
*/
68-
public function getLatestReceivedReviews(?Model $model = null, bool $includeUnapproved = false): Collection
67+
public function latestReceivedReviews(?Model $model = null, bool $includeUnapproved = false): HasMany
6968
{
7069
$query = $this->receivedReviews($model);
7170

7271
if ($includeUnapproved) {
7372
$query->withUnapproved();
7473
}
7574

76-
return $query->orderByDesc('created_at')->get();
75+
return $query->orderByDesc('created_at');
7776
}
7877

7978
/**

tests/CanBeReviewedTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,37 +148,37 @@ public function test_getReceivedReview_can_retrieve_unapproved_review_when_inclu
148148
$this->assertEquals(3, $review->rating);
149149
}
150150

151-
public function test_getLatestReceivedReviews_retrieves_latest_approved_reviews_by_default()
151+
public function test_latestReceivedReviews_retrieves_latest_approved_reviews_by_default()
152152
{
153153
$newMentee = Mentor::factory()->create();
154154

155155
$newReview = $newMentee->review($this->mentor, 4, 'nice!!!');
156156

157-
$reviews = $this->mentor->getLatestReceivedReviews();
157+
$reviews = $this->mentor->latestReceivedReviews()->get();
158158

159159
$this->assertCount(3, $reviews);
160160
$this->assertEquals($reviews->first()->id, $newReview->id);
161161
}
162162

163-
public function test_getLatestReceivedReviews_not_returning_unapproved_reviews_by_default()
163+
public function test_latestReceivedReviews_not_returning_unapproved_reviews_by_default()
164164
{
165165
$newMentee = Mentor::factory()->create();
166166

167167
$newReview = $newMentee->review($this->mentor, 4, 'nice!!!', isApproved: false);
168168

169-
$reviews = $this->mentor->getLatestReceivedReviews();
169+
$reviews = $this->mentor->latestReceivedReviews()->get();
170170

171171
$this->assertCount(2, $reviews);
172172
$this->assertNotEquals($reviews->first()->id, $newReview->id);
173173
}
174174

175-
public function test_getLatestReceivedReviews_can_retrieves_unapproved_reviews_when_includeUnapproved_is_true()
175+
public function test_latestReceivedReviews_can_retrieves_unapproved_reviews_when_includeUnapproved_is_true()
176176
{
177177
$newMentee = Mentor::factory()->create();
178178

179179
$newReview = $newMentee->review($this->mentor, 4, 'nice!!!', isApproved: false);
180180

181-
$reviews = $this->mentor->getLatestReceivedReviews(includeUnapproved: true);
181+
$reviews = $this->mentor->latestReceivedReviews(includeUnapproved: true)->get();
182182

183183
$this->assertCount(3, $reviews);
184184
$this->assertEquals($reviews->first()->id, $newReview->id);

0 commit comments

Comments
 (0)