1 | <?php |
||
13 | trait Rateable |
||
14 | { |
||
15 | /** |
||
16 | * This model has many ratings. |
||
17 | * |
||
18 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
19 | */ |
||
20 | public function ratings() |
||
24 | |||
25 | public function averageRating() |
||
29 | |||
30 | public function countRating() |
||
34 | |||
35 | public function totalRating() |
||
39 | |||
40 | public function averageRatingForUser(int $user_id) |
||
44 | |||
45 | public function totalRatingForUser(int $user_id) |
||
49 | |||
50 | public function countRatingForUser(int $user_id) |
||
54 | |||
55 | public function ratingPercentage() |
||
65 | |||
66 | public function positiveRatingCount() |
||
70 | |||
71 | public function positiveRatingTotal() |
||
75 | |||
76 | public function negativeRatingCount() |
||
80 | |||
81 | public function negativeRatingTotal() |
||
85 | |||
86 | public function isRated() |
||
90 | |||
91 | public function isRatedBy(int $user_id) |
||
95 | |||
96 | /** |
||
97 | * to order by average_rating. |
||
98 | * |
||
99 | * add protected $appends = [ 'average_rating' ]; to your model |
||
100 | * |
||
101 | * Lesson::all()->sortBy('average_rating') |
||
102 | * Lesson::with('relatedModel')->get()->sortBy('average_rating') |
||
103 | * Lesson::where('status', 'published')->get()->sortBy('average_rating') |
||
104 | * |
||
105 | * @return mixed |
||
106 | */ |
||
107 | public function getAverageRatingAttribute() |
||
111 | |||
112 | public function scopeOrderByAverageRating(Builder $query, string $direction = 'asc') |
||
124 | |||
125 | public function deleteRating(int $rating_id) |
||
129 | |||
130 | public function resetRating() |
||
134 | |||
135 | public function deleteRatingsForUser(int $user_id) |
||
139 | |||
140 | /** |
||
141 | * @param int $user_id |
||
142 | * @param int $value |
||
143 | * |
||
144 | * @return int |
||
145 | * |
||
146 | * @throws \Throwable |
||
147 | */ |
||
148 | public function updateRatingForUser(int $user_id, int $value) |
||
154 | |||
155 | /** |
||
156 | * @param int $rating_id |
||
157 | * @param int $value |
||
158 | * |
||
159 | * @return int |
||
160 | * |
||
161 | * @throws \Throwable |
||
162 | */ |
||
163 | public function updateRating(int $rating_id, int $value) |
||
169 | |||
170 | /** |
||
171 | * @return RatingBuilder |
||
172 | * |
||
173 | * @throws \Throwable |
||
174 | */ |
||
175 | public function getRatingBuilder() |
||
180 | |||
181 | public function raters() |
||
185 | |||
186 | public function countRatingsByDate($from = null, $to = null) |
||
200 | |||
201 | public function getRatingQueryBuilder() |
||
205 | } |
||
206 |
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.