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 | public function votesCount() |
||
25 | { |
||
26 | return $this->votes()->sum('amount'); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @return mixed |
||
31 | */ |
||
32 | public function upVotesCount() |
||
33 | { |
||
34 | return $this->votes()->where('amount', '>=', 0)->sum('amount'); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return mixed |
||
39 | */ |
||
40 | public function downVotesCount() |
||
44 | |||
45 | public function isVoted() |
||
46 | { |
||
49 | |||
50 | /** |
||
51 | * @return bool |
||
52 | */ |
||
53 | public function isUpVoted() |
||
57 | |||
58 | /** |
||
59 | * @return bool |
||
60 | */ |
||
61 | public function isDownVoted() |
||
65 | |||
66 | public function isVotedByUser(int $user_id) |
||
70 | |||
71 | /** |
||
72 | * @param int $user_id |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function isUpVotedByUser(int $user_id) |
||
80 | |||
81 | /** |
||
82 | * @param int $user_id |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function isDownVotedByUser(int $user_id) |
||
90 | |||
91 | /** |
||
92 | * @param Builder $query |
||
93 | * @param string $direction |
||
94 | * @param string $type |
||
95 | * |
||
96 | * @return Builder |
||
97 | */ |
||
98 | public function scopeOrderByUpVotes(Builder $query, string $direction = 'asc', string $type = '>=') |
||
111 | |||
112 | /** |
||
113 | * @param Builder $query |
||
114 | * @param string $direction |
||
115 | * |
||
116 | * @return Builder |
||
117 | */ |
||
118 | public function scopeOrderByDownVotes(Builder $query, string $direction = 'asc') |
||
122 | |||
123 | /** |
||
124 | * @param int $vote_id |
||
125 | * |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public function cancelVote(int $vote_id) |
||
132 | |||
133 | /** |
||
134 | * @return mixed |
||
135 | */ |
||
136 | public function resetVotes() |
||
140 | |||
141 | /** |
||
142 | * @param int $user_id |
||
143 | * |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function cancelVotesForUser(int $user_id) |
||
150 | |||
151 | /** |
||
152 | * @param int $user_id |
||
153 | * @param int $amount |
||
154 | * |
||
155 | * @return int |
||
156 | */ |
||
157 | public function updateVotesForUser(int $user_id, int $amount) |
||
161 | |||
162 | /** |
||
163 | * @param int $vote_id |
||
164 | * @param int $amount |
||
165 | * |
||
166 | * @return int |
||
167 | */ |
||
168 | public function updateVote(int $vote_id, int $amount) |
||
172 | |||
173 | /** |
||
174 | * @return VoteBuilder |
||
175 | * |
||
176 | * @throws \Throwable |
||
177 | */ |
||
178 | public function getVoteBuilder() |
||
183 | |||
184 | public function voters() |
||
188 | |||
189 | public function countVotesByDate($from = null, $to = null) |
||
203 | } |
||
204 |
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.