1 | <?php |
||
12 | trait Voteable |
||
13 | { |
||
14 | /** |
||
15 | * This model has many votes. |
||
16 | * |
||
17 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
18 | */ |
||
19 | public function votes() |
||
23 | |||
24 | /** |
||
25 | * @return mixed |
||
26 | */ |
||
27 | public function upVotesCount() |
||
31 | |||
32 | /** |
||
33 | * @return mixed |
||
34 | */ |
||
35 | public function downVotesCount() |
||
39 | |||
40 | /** |
||
41 | * @return bool |
||
42 | */ |
||
43 | public function isUpVoted() |
||
44 | { |
||
45 | return $this->votes()->where('amount', '>=', 0)->exists(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function isDownVoted() |
||
55 | |||
56 | public function isVotedByUser(int $user_id) |
||
60 | |||
61 | /** |
||
62 | * @param int $user_id |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function isUpVotedByUser(int $user_id) |
||
70 | |||
71 | /** |
||
72 | * @param int $user_id |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function isDownVotedByUser(int $user_id) |
||
80 | |||
81 | /** |
||
82 | * @param Builder $query |
||
83 | * @param string $direction |
||
84 | * @param string $type |
||
85 | * |
||
86 | * @return Builder |
||
87 | */ |
||
88 | public function scopeOrderByUpVotes(Builder $query, string $direction = 'asc', string $type = '>=') |
||
101 | |||
102 | /** |
||
103 | * @param Builder $query |
||
104 | * @param string $direction |
||
105 | * |
||
106 | * @return Builder |
||
107 | */ |
||
108 | public function scopeOrderByDownVotes(Builder $query, string $direction = 'asc') |
||
112 | |||
113 | /** |
||
114 | * @param int $vote_id |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function cancelVote(int $vote_id) |
||
122 | |||
123 | /** |
||
124 | * @return mixed |
||
125 | */ |
||
126 | public function resetVotes() |
||
130 | |||
131 | /** |
||
132 | * @param int $user_id |
||
133 | * |
||
134 | * @return mixed |
||
135 | */ |
||
136 | public function cancelVotesForUser(int $user_id) |
||
140 | |||
141 | /** |
||
142 | * @param int $user_id |
||
143 | * @param int $amount |
||
144 | * |
||
145 | * @return int |
||
146 | */ |
||
147 | public function updateVotesForUser(int $user_id, int $amount) |
||
151 | |||
152 | /** |
||
153 | * @param int $vote_id |
||
154 | * @param int $amount |
||
155 | * |
||
156 | * @return int |
||
157 | */ |
||
158 | public function updateVote(int $vote_id, int $amount) |
||
162 | |||
163 | /** |
||
164 | * @return VoteBuilder |
||
165 | * |
||
166 | * @throws \Throwable |
||
167 | */ |
||
168 | public function getVoteBuilder() |
||
173 | |||
174 | public function voters() |
||
183 | } |
||
184 |
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.