Skip to content

Commit 8e2e064

Browse files
committed
Added getAllRatings method to retrieve all ratings for a particular resource.
1 parent de708a7 commit 8e2e064

File tree

13 files changed

+615
-7
lines changed

13 files changed

+615
-7
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ or
115115
$post->averageRating(2) //round to 2 decimal place
116116
````
117117

118+
### Get all ratings:
119+
```php
120+
$post = Post::first();
121+
122+
$ratings = $post->getAllRatings($post->id);
123+
```
124+
118125
### Count total rating:
119126
````php
120127
$post->countRating()

database/migrations/2018_10_24_10000_create_reviews_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function up()
1414
$table->integer('quality_rating')->nullable();
1515
$table->integer('friendly_rating')->nullable();
1616
$table->integer('price_rating')->nullable();
17-
$table->string('recommend', 3)->nullable();
17+
$table->enum('recommend', ['Yes', 'No']);
1818
$table->enum('department', ['Sales', 'Service', 'Parts']);
1919
$table->string('title');
2020
$table->string('body');

src/Contracts/ReviewRateable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ public function updateRating($id, $data, Model $parent = null);
110110
/**
111111
*
112112
* @param $id
113+
* @param $sort
113114
* @return mixed
114115
*/
115-
public function getRatings($id);
116+
public function getAllRatings($id, $sort = 'desc');
116117

117118
/**
118119
*

src/Models/Rating.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ public function updateRating($id, $data)
6868

6969
/**
7070
* @param $id
71-
*
71+
* @param $sort
7272
* @return mixed
7373
*/
74-
public function getRatings($id)
74+
public function getAllRatings($id, $sort = 'desc')
7575
{
76-
$rating = $this->all()->where('reviewrateable_id', $id);
76+
$rating = $this->select('*')
77+
->where('reviewrateable_id', $id)
78+
->orderBy('created_at', $sort)
79+
->get();
7780

7881
return $rating;
7982
}

src/Traits/ReviewRateable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,12 @@ public function updateRating($id, $data, Model $parent = null)
209209
/**
210210
*
211211
* @param $id
212+
* @param $sort
212213
* @return mixed
213214
*/
214-
public function getRatings($id)
215+
public function getAllRatings($id, $sort = 'desc')
215216
{
216-
return (new Rating())->getRatings($id);
217+
return (new Rating())->getAllRatings($id, $sort);
217218
}
218219

219220
/**

vendor/autoload.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
require_once __DIR__ . '/composer/autoload_real.php';
6+
7+
return ComposerAutoloaderInitfa0198145354feb06b170b448a570927::getLoader();

0 commit comments

Comments
 (0)