1 | <?php |
||
12 | trait Rateable |
||
13 | { |
||
14 | /** |
||
15 | * This model has many ratings. |
||
16 | * |
||
17 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
18 | */ |
||
19 | public function ratings() |
||
23 | |||
24 | public function averageRating() |
||
28 | |||
29 | public function countRating() |
||
33 | |||
34 | public function totalRating() |
||
38 | |||
39 | public function averageRatingForUser(int $user_id) |
||
43 | |||
44 | public function totalRatingForUser(int $user_id) |
||
48 | |||
49 | public function countRatingForUser(int $user_id) |
||
53 | |||
54 | public function ratingPercentage() |
||
64 | |||
65 | public function positiveRatingCount() |
||
69 | |||
70 | public function positiveRatingTotal() |
||
74 | |||
75 | public function negativeRatingCount() |
||
79 | |||
80 | public function negativeRatingTotal() |
||
84 | |||
85 | public function isRated() |
||
89 | |||
90 | public function isRatedBy(int $user_id) |
||
94 | |||
95 | /** |
||
96 | * to order by average_rating. |
||
97 | * |
||
98 | * add protected $appends = [ 'average_rating' ]; to your model |
||
99 | * |
||
100 | * Lesson::all()->sortBy('average_rating') |
||
101 | * Lesson::with('relatedModel')->get()->sortBy('average_rating') |
||
102 | * Lesson::where('status', 'published')->get()->sortBy('average_rating') |
||
103 | * |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public function getAverageRatingAttribute() |
||
110 | |||
111 | public function scopeOrderByAverageRating(Builder $query, string $direction = 'asc') |
||
123 | |||
124 | public function deleteRating(int $rating_id) |
||
128 | |||
129 | public function resetRating() |
||
133 | |||
134 | public function deleteRatingsForUser(int $user_id) |
||
138 | |||
139 | /** |
||
140 | * @param int $user_id |
||
141 | * @param int $value |
||
142 | * |
||
143 | * @return int |
||
144 | * |
||
145 | * @throws \Throwable |
||
146 | */ |
||
147 | public function updateRatingForUser(int $user_id, int $value) |
||
153 | |||
154 | /** |
||
155 | * @param int $rating_id |
||
156 | * @param int $value |
||
157 | * |
||
158 | * @return int |
||
159 | * |
||
160 | * @throws \Throwable |
||
161 | */ |
||
162 | public function updateRating(int $rating_id, int $value) |
||
168 | |||
169 | /** |
||
170 | * @return RatingBuilder |
||
171 | * |
||
172 | * @throws \Throwable |
||
173 | */ |
||
174 | public function getRatingBuilder() |
||
179 | |||
180 | public function raters() |
||
184 | |||
185 | public function countRatingsByDate($from = null, $to = null) |
||
199 | |||
200 | public function getRatingQueryBuilder() |
||
204 | } |
||
205 |
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.