Skip to content

Commit

Permalink
Merge pull request #11 from codebyray/develop
Browse files Browse the repository at this point in the history
Added ability to get recent user ratings
  • Loading branch information
codebyray authored Oct 24, 2019
2 parents 89a201f + 4cfb5cd commit 36935b6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ $ratings = $post->getAllRatings($post->id, 'desc');
// Limit default is 5, sort default is desc
$ratings = $post->getRecentRatings($post->id, 5, 'desc');

// Get the most recent user ratings (limit and sort are optional)
// Limit default is 5, approved default is true, sort default is desc
$userRatings = $post->getRecentUserRatings($user->id, 5, true, 'desc');

```
### Fetch the average rating:
````php
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
"minimum-stability": "dev",
"require": {
"php" : "^7.2",
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0.0"
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^6.3|^7.0|^8.0",
"orchestra/testbench": "~3.5.0|~3.6.0|^4.0"
},
"autoload": {
"psr-4": {
"Codebyray\\ReviewRateable\\": "src/"
"Codebyray\\ReviewRateable\\": "src"
}
},
"autoload-dev": {
Expand Down
9 changes: 9 additions & 0 deletions src/Contracts/ReviewRateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ public function getNotApprovedRatings($id, $sort = 'desc');
*/
public function getRecentRatings($id, $limit = 5, $sort = 'desc');

/**
* @param $id
* @param $limit
* @param $approved
* @param $sort
* @return mixed
*/
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc');

/**
*
* @param $id
Expand Down
19 changes: 19 additions & 0 deletions src/Models/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ public function getRecentRatings($id, $limit = 5, $sort = 'desc')
return $rating;
}

/**
* @param $id
* @param $limit
* @param $approved
* @param $sort
* @return mixed
*/
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc')
{
$rating = $this->select('*')
->where('author_id', $id)
->where('approved', $approved)
->orderBy('created_at', $sort)
->limit($limit)
->get();

return $rating;
}

/**
* @param $id
*
Expand Down
12 changes: 12 additions & 0 deletions src/Traits/ReviewRateable.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ public function getRecentRatings($id, $limit = 5, $sort = 'desc')
return (new Rating())->getRecentRatings($id, $limit, $sort);
}

/**
* @param $id
* @param $limit
* @param $approved
* @param $sort
* @return mixed
*/
public function getRecentUserRatings($id, $limit = 5, $approved = true, $sort = 'desc')
{
return (new Rating())->getRecentUserRatings($id, $limit, $approved, $sort);
}

/**
* @param $id
*
Expand Down

0 comments on commit 36935b6

Please sign in to comment.