1 | <?php |
||
11 | trait Commentable |
||
12 | { |
||
13 | /** |
||
14 | * This model has many comments. |
||
15 | * |
||
16 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
17 | */ |
||
18 | public function comments() |
||
22 | |||
23 | public function commentsCount() |
||
27 | |||
28 | public function isCommented() |
||
32 | |||
33 | public function isCommentedByUser(int $user_id) |
||
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) |
||
55 | |||
56 | public function resetComments() |
||
60 | |||
61 | public function deleteCommentsForUser(int $user_id) |
||
65 | |||
66 | public function updateComment(int $comment_id, string $comment) |
||
70 | |||
71 | public function getCommentBuilder() |
||
76 | |||
77 | /** |
||
78 | * @return CommentQueryBuilder |
||
79 | */ |
||
80 | public function getCommentQueryBuilder() |
||
84 | |||
85 | public function commenters() |
||
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.