Skip to content

Commit e1392da

Browse files
committed
Added ability to set reviews/ratings as approved or not for moderation
1 parent 23b6e26 commit e1392da

File tree

5 files changed

+94
-5
lines changed

5 files changed

+94
-5
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class Post extends Model implements ReviewRateable
5252
}
5353
```
5454

55-
### Create a rating``
55+
### Create a rating
56+
When creating a rating you can specify whether the rating is approved or not by adding approved to the array. This is optional and if left
57+
out the default is not approved to allow for review before posting.
5658
```php
5759
$user = User::first();
5860
$post = Post::first();
@@ -66,6 +68,7 @@ $rating = $post->rating([
6668
'price_rating' => 5,
6769
'rating' => 5,
6870
'recommend' => 'Yes',
71+
'approved' => true, // This is optional and defaults to false
6972
], $user);
7073

7174
dd($rating);
@@ -82,14 +85,29 @@ $rating = $post->updateRating(1, [
8285
'price_rating' => 4,
8386
'rating' => 4,
8487
'recommend' => 'No',
88+
'approved' => true, // This is optional and defaults to false
8589
]);
8690
```
87-
91+
### Marking review as approved
92+
```php
93+
$rating = $post->updateRating(1, ['approved' => true]);
94+
```
8895
### Delete a rating:
8996
```php
9097
$post->deleteRating(1);
9198
```
9299

100+
### Fetch approved or not approved reviews/ratings for a particular resource
101+
```php
102+
// Get not approved ratings
103+
$ratings = $post->getApprovedRatings($post->id, 'desc');
104+
105+
// Get not approved ratings
106+
$ratings = $post->getNotApprovedRatings($post->id, 'desc');
107+
108+
// Get all ratings whether approved or not
109+
$ratings = $post->getAllRatings($post->id, 'desc');
110+
```
93111
### Fetch the average rating:
94112
````php
95113
// Get Overall Average Rating

database/migrations/create_reviews_table.php.stub

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class CreateReviewsTable extends Migration
88
public function up()
99
{
1010
Schema::create('reviews', function (Blueprint $table) {
11-
$table->increments('id');
11+
$table->bigIncrements('id');
1212
$table->integer('rating');
1313
$table->integer('customer_service_rating')->nullable();
1414
$table->integer('quality_rating')->nullable();
@@ -18,6 +18,7 @@ class CreateReviewsTable extends Migration
1818
$table->enum('department', ['Sales', 'Service', 'Parts']);
1919
$table->string('title');
2020
$table->string('body');
21+
$table->boolean('approved')->default(0);
2122
$table->morphs('reviewrateable');
2223
$table->morphs('author');
2324
$table->timestamps();
@@ -26,6 +27,6 @@ class CreateReviewsTable extends Migration
2627

2728
public function down()
2829
{
29-
Schema::dropIfExists('ratings');
30+
Schema::dropIfExists('reviews');
3031
}
3132
}

src/Contracts/ReviewRateable.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,29 @@ public function rating($data, Model $author, Model $parent = null);
107107
*/
108108
public function updateRating($id, $data, Model $parent = null);
109109

110+
/**
111+
*
112+
* @param $id
113+
* @param $sort
114+
* @return mixed
115+
*/
116+
public function getAllRatings($id, $sort = 'desc');
117+
110118
/**
111119
*
112120
* @param $id
113121
* @param $sort
114122
* @return mixed
115123
*/
116-
public function getAllRatings($id, $sort = 'desc');
124+
public function getApprovedRatings($id, $sort = 'desc');
125+
126+
/**
127+
*
128+
* @param $id
129+
* @param $sort
130+
* @return mixed
131+
*/
132+
public function getNotApprovedRatings($id, $sort = 'desc');
117133

118134
/**
119135
*

src/Models/Rating.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@ public function getAllRatings($id, $sort = 'desc')
8181
return $rating;
8282
}
8383

84+
/**
85+
* @param $id
86+
* @param $sort
87+
* @return mixed
88+
*/
89+
public function getApprovedRatings($id, $sort = 'desc')
90+
{
91+
$rating = $this->select('*')
92+
->where('reviewrateable_id', $id)
93+
->where('approved', true)
94+
->orderBy('created_at', $sort)
95+
->get();
96+
97+
return $rating;
98+
}
99+
100+
/**
101+
* @param $id
102+
* @param $sort
103+
* @return mixed
104+
*/
105+
public function getNotApprovedRatings($id, $sort = 'desc')
106+
{
107+
$rating = $this->select('*')
108+
->where('reviewrateable_id', $id)
109+
->where('approved', false)
110+
->orderBy('created_at', $sort)
111+
->get();
112+
113+
return $rating;
114+
}
115+
84116
/**
85117
* @param $id
86118
*

src/Traits/ReviewRateable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,28 @@ public function getAllRatings($id, $sort = 'desc')
217217
return (new Rating())->getAllRatings($id, $sort);
218218
}
219219

220+
/**
221+
*
222+
* @param $id
223+
* @param $sort
224+
* @return mixed
225+
*/
226+
public function getApprovedRatings($id, $sort = 'desc')
227+
{
228+
return (new Rating())->getApprovedRatings($id, $sort);
229+
}
230+
231+
/**
232+
*
233+
* @param $id
234+
* @param $sort
235+
* @return mixed
236+
*/
237+
public function getNotApprovedRatings($id, $sort = 'desc')
238+
{
239+
return (new Rating())->getNotApprovedRatings($id, $sort);
240+
}
241+
220242
/**
221243
* @param $id
222244
*

0 commit comments

Comments
 (0)