Skip to content

Commit

Permalink
Add commentable_type param to getNewestComments helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
iooe committed Jan 5, 2019
1 parent 1f36da8 commit 28d81c3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ If you open the page containing the view where you have placed the above code, y
` use tizis\laraComments\Http\CommentsHelper;`
#### Methods:
- getNewestComments($take = 10)
- getNewestComments(default $take = 10, default $commentable_type = null)
- ...
#### Example:
` CommentsHelper::getNewestComments(10) // Return last 10 comments`
```
CommentsHelper::getNewestComments(20) // Return last 20 comments
CommentsHelper::getNewestComments(20, Book::class) // Return last 20 comments of Book model
```
4 changes: 2 additions & 2 deletions src/Http/CommentsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

class CommentsHelper
{
public static function getNewestComments($take = 10)
public static function getNewestComments($take = 10, $commentable_type = null)
{
return CommentResource::collection(CommentService::getNewestComments($take));
return CommentResource::collection(CommentService::getNewestComments($take, $commentable_type));
}
}
8 changes: 7 additions & 1 deletion src/UseCases/CommentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ class CommentService
{
/**
* @param int $take
* @param null $commentable_type
* @return mixed
*/
public static function getNewestComments($take = 10) {
public static function getNewestComments($take = 10, $commentable_type = null)
{
return Comment::take($take)
->when($commentable_type !== null, function ($q) use ($commentable_type) {
return $q->where('commentable_type', $commentable_type);
})
->with(['commentable'])
->orderBy('id', 'desc')
->get();
}

/**
* @param $message
* @return mixed
Expand Down

0 comments on commit 28d81c3

Please sign in to comment.