File tree 4 files changed +49
-0
lines changed
4 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -172,6 +172,20 @@ foreach($user->reputations as $reputation) {
172
172
}
173
173
```
174
174
175
+ If you want to get all the points given on a ` subject ` model. You should define a ` morphMany ` relations. For example on post model.
176
+
177
+ ``` php
178
+ /**
179
+ * Get all the post's reputation.
180
+ */
181
+ public function reputations()
182
+ {
183
+ return $this->morphMany('QCod\Gamify\Reputation', 'subject');
184
+ }
185
+ ```
186
+
187
+ Now you can get all the reputation given on a ` Post ` using ` $post->reputations ` .
188
+
175
189
### Configure a Point Type
176
190
177
191
#### Point payee
Original file line number Diff line number Diff line change @@ -18,6 +18,16 @@ public function payee()
18
18
return $ this ->belongsTo (config ('gamify.payee_model ' ), 'payee_id ' );
19
19
}
20
20
21
+ /**
22
+ * Get the owning subject model
23
+ *
24
+ * @return \Illuminate\Database\Eloquent\Relations\MorphTo
25
+ */
26
+ public function subject ()
27
+ {
28
+ return $ this ->morphTo ();
29
+ }
30
+
21
31
/**
22
32
* Undo last point
23
33
*
Original file line number Diff line number Diff line change @@ -25,4 +25,9 @@ public function bestReply()
25
25
{
26
26
return $ this ->hasOne (Reply::class, 'id ' , 'best_reply_id ' );
27
27
}
28
+
29
+ public function reputations ()
30
+ {
31
+ return $ this ->morphMany ('QCod\Gamify\Reputation ' , 'subject ' );
32
+ }
28
33
}
Original file line number Diff line number Diff line change @@ -69,6 +69,26 @@ public function it_gives_point_to_a_user()
69
69
]);
70
70
}
71
71
72
+ /**
73
+ * it can access a reputation payee and subject
74
+ *
75
+ * @test
76
+ */
77
+ public function it_can_access_a_reputation_payee_and_subject ()
78
+ {
79
+ $ user = $ this ->createUser ();
80
+ $ post = $ this ->createPost (['user_id ' => $ user ->id ]);
81
+
82
+ $ user ->givePoint (new FakeCreatePostPoint ($ post ));
83
+
84
+ $ point = $ user ->reputations ()->first ();
85
+
86
+ $ this ->assertEquals ($ user ->id , $ point ->payee ->id );
87
+ $ this ->assertEquals ($ post ->id , $ point ->subject ->id );
88
+
89
+ $ this ->assertEquals ('FakeCreatePostPoint ' , $ post ->reputations ->first ()->name );
90
+ }
91
+
72
92
/**
73
93
* it only adds unique point reward if property is set on point type
74
94
*
You can’t perform that action at this time.
0 commit comments