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 | /** |
||
57 | * @param int $user_id |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | public function isUpVotedByUser(int $user_id) |
||
65 | |||
66 | /** |
||
67 | * @param int $user_id |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | public function isDownVotedByUser(int $user_id) |
||
75 | |||
76 | /** |
||
77 | * @param Builder $query |
||
78 | * @param string $direction |
||
79 | * @param string $type |
||
80 | * |
||
81 | * @return Builder |
||
82 | */ |
||
83 | public function scopeOrderByUpVotes(Builder $query, string $direction = 'asc', string $type = '>=') |
||
96 | |||
97 | /** |
||
98 | * @param Builder $query |
||
99 | * @param string $direction |
||
100 | * |
||
101 | * @return Builder |
||
102 | */ |
||
103 | public function scopeOrderByDownVotes(Builder $query, string $direction = 'asc') |
||
107 | |||
108 | /** |
||
109 | * @param int $vote_id |
||
110 | * |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function cancelVote(int $vote_id) |
||
114 | { |
||
115 | return $this->votes()->where('id', $vote_id)->delete(); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function resetVotes() |
||
125 | |||
126 | /** |
||
127 | * @param int $user_id |
||
128 | * |
||
129 | * @return mixed |
||
130 | */ |
||
131 | public function cancelVotesForUser(int $user_id) |
||
132 | { |
||
133 | return $this->votes()->where('user_id', $user_id)->delete(); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param int $user_id |
||
138 | * @param int $amount |
||
139 | * |
||
140 | * @return int |
||
141 | */ |
||
142 | public function updateVotesForUser(int $user_id, int $amount) |
||
146 | |||
147 | /** |
||
148 | * @param int $vote_id |
||
149 | * @param int $amount |
||
150 | * |
||
151 | * @return int |
||
152 | */ |
||
153 | public function updateVote(int $vote_id, int $amount) |
||
154 | { |
||
155 | return $this->votes()->where('id', $vote_id)->update(['amount' => $amount]); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return VoteBuilder |
||
160 | * |
||
161 | * @throws \Throwable |
||
162 | */ |
||
163 | public function getRatingBuilder() |
||
168 | |||
169 | public function voters() |
||
170 | { |
||
171 | $user = config('voteable.user'); |
||
172 | |||
178 | } |
||
179 |
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.