Skip to content

Commit 4cfb5cd

Browse files
committed
Added ability to get recent user ratings
1 parent 2462761 commit 4cfb5cd

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ $ratings = $post->getAllRatings($post->id, 'desc');
112112
// Limit default is 5, sort default is desc
113113
$ratings = $post->getRecentRatings($post->id, 5, 'desc');
114114

115+
// Get the most recent user ratings (limit and sort are optional)
116+
// Limit default is 5, approved default is true, sort default is desc
117+
$userRatings = $post->getRecentUserRatings($user->id, 5, true, 'desc');
118+
115119
```
116120
### Fetch the average rating:
117121
````php

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
"minimum-stability": "dev",
1313
"require": {
1414
"php" : "^7.2",
15-
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0.0",
16-
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0.0"
15+
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
16+
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0"
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "^6.3|^7.0|^8.0",
2020
"orchestra/testbench": "~3.5.0|~3.6.0|^4.0"
2121
},
2222
"autoload": {
2323
"psr-4": {
24-
"Codebyray\\ReviewRateable\\": "src/"
24+
"Codebyray\\ReviewRateable\\": "src"
2525
}
2626
},
2727
"autoload-dev": {

src/Contracts/ReviewRateable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ public function getNotApprovedRatings($id, $sort = 'desc');
139139
*/
140140
public function getRecentRatings($id, $limit = 5, $sort = 'desc');
141141

142+
/**
143+
* @param $id
144+
* @param $limit
145+
* @param $approved
146+
* @param $sort
147+
* @return mixed
148+
*/
149+
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc');
150+
142151
/**
143152
*
144153
* @param $id

src/Models/Rating.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,25 @@ public function getRecentRatings($id, $limit = 5, $sort = 'desc')
131131
return $rating;
132132
}
133133

134+
/**
135+
* @param $id
136+
* @param $limit
137+
* @param $approved
138+
* @param $sort
139+
* @return mixed
140+
*/
141+
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc')
142+
{
143+
$rating = $this->select('*')
144+
->where('author_id', $id)
145+
->where('approved', $approved)
146+
->orderBy('created_at', $sort)
147+
->limit($limit)
148+
->get();
149+
150+
return $rating;
151+
}
152+
134153
/**
135154
* @param $id
136155
*

src/Traits/ReviewRateable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ public function getRecentRatings($id, $limit = 5, $sort = 'desc')
250250
return (new Rating())->getRecentRatings($id, $limit, $sort);
251251
}
252252

253+
/**
254+
* @param $id
255+
* @param $limit
256+
* @param $approved
257+
* @param $sort
258+
* @return mixed
259+
*/
260+
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc')
261+
{
262+
return (new Rating())->getRecentUserRatings($id, $limit, $approved, $sort);
263+
}
264+
253265
/**
254266
* @param $id
255267
*

0 commit comments

Comments
 (0)