1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yoeunes\Commentable\Traits; |
4
|
|
|
|
5
|
|
|
use Yoeunes\Commentable\CommentBuilder; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Query\JoinClause; |
8
|
|
|
use Yoeunes\Commentable\CommentQueryBuilder; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
10
|
|
|
|
11
|
|
|
trait Commentable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* This model has many comments. |
15
|
|
|
* |
16
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
17
|
|
|
*/ |
18
|
|
|
public function comments() |
19
|
|
|
{ |
20
|
|
|
return $this->morphMany(config('commentable.comment'), 'commentable'); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function commentsCount() |
24
|
|
|
{ |
25
|
|
|
return $this->comments()->count(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function isCommented() |
29
|
|
|
{ |
30
|
|
|
return $this->comments()->exists(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function isCommentedByUser(int $user_id) |
34
|
|
|
{ |
35
|
|
|
return $this->comments()->where('user_id', $user_id)->exists(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function scopeOrderByCommentsCount(Builder $query, string $direction = 'asc') |
39
|
|
|
{ |
40
|
|
|
return $query |
41
|
|
|
->leftJoin('comments', function (JoinClause $join) { |
42
|
|
|
$join |
43
|
|
|
->on('comments.commentable_id', $this->getTable() . '.id') |
|
|
|
|
44
|
|
|
->where('comments.commentable_type', in_array(__CLASS__, Relation::morphMap()) ? array_search(__CLASS__, Relation::morphMap()) : __CLASS__); |
45
|
|
|
}) |
46
|
|
|
->addSelect(DB::raw('COUNT(comments.id) as count_comments')) |
47
|
|
|
->groupBy($this->getTable(). '.id') |
|
|
|
|
48
|
|
|
->orderBy('count_comments', $direction); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function deleteComment(int $comment_id) |
52
|
|
|
{ |
53
|
|
|
return $this->comments()->where('id', $comment_id)->delete(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function resetComments() |
57
|
|
|
{ |
58
|
|
|
return $this->comments()->delete(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function deleteCommentsForUser(int $user_id) |
62
|
|
|
{ |
63
|
|
|
return $this->comments()->where('user_id', $user_id)->delete(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function updateComment(int $comment_id, string $comment) |
67
|
|
|
{ |
68
|
|
|
return $this->comments()->where('id', $comment_id)->update(['comment' => $comment]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getCommentBuilder() |
72
|
|
|
{ |
73
|
|
|
return (new CommentBuilder()) |
74
|
|
|
->commentable($this); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return CommentQueryBuilder |
79
|
|
|
*/ |
80
|
|
|
public function getCommentQueryBuilder() |
81
|
|
|
{ |
82
|
|
|
return new CommentQueryBuilder($this->comments()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function commenters() |
86
|
|
|
{ |
87
|
|
|
return $this->morphToMany(config('commentable.user'), 'commentable', 'comments'); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.