From 747986ce6eec3fec8ab047d0a5d05d70b3ed27a8 Mon Sep 17 00:00:00 2001 From: Mohd Saqueib Ansari Date: Sun, 17 Mar 2019 06:51:02 +0530 Subject: [PATCH] added subject relation on reputation, closes #7 --- README.md | 14 ++++++++++++++ src/Reputation.php | 10 ++++++++++ tests/Models/Post.php | 5 +++++ tests/PointTest.php | 20 ++++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/README.md b/README.md index c0bca6e..062d0e4 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,20 @@ foreach($user->reputations as $reputation) { } ``` +If you want to get all the points given on a `subject` model. You should define a `morphMany` relations. For example on post model. + +```php + /** + * Get all the post's reputation. + */ + public function reputations() + { + return $this->morphMany('QCod\Gamify\Reputation', 'subject'); + } +``` + +Now you can get all the reputation given on a `Post` using `$post->reputations`. + ### Configure a Point Type #### Point payee diff --git a/src/Reputation.php b/src/Reputation.php index fb810db..e55ada8 100644 --- a/src/Reputation.php +++ b/src/Reputation.php @@ -18,6 +18,16 @@ public function payee() return $this->belongsTo(config('gamify.payee_model'), 'payee_id'); } + /** + * Get the owning subject model + * + * @return \Illuminate\Database\Eloquent\Relations\MorphTo + */ + public function subject() + { + return $this->morphTo(); + } + /** * Undo last point * diff --git a/tests/Models/Post.php b/tests/Models/Post.php index 0c1c26d..ffbf963 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -25,4 +25,9 @@ public function bestReply() { return $this->hasOne(Reply::class, 'id', 'best_reply_id'); } + + public function reputations() + { + return $this->morphMany('QCod\Gamify\Reputation', 'subject'); + } } diff --git a/tests/PointTest.php b/tests/PointTest.php index 3b0f51a..13443e3 100644 --- a/tests/PointTest.php +++ b/tests/PointTest.php @@ -69,6 +69,26 @@ public function it_gives_point_to_a_user() ]); } + /** + * it can access a reputation payee and subject + * + * @test + */ + public function it_can_access_a_reputation_payee_and_subject() + { + $user = $this->createUser(); + $post = $this->createPost(['user_id' => $user->id]); + + $user->givePoint(new FakeCreatePostPoint($post)); + + $point = $user->reputations()->first(); + + $this->assertEquals($user->id, $point->payee->id); + $this->assertEquals($post->id, $point->subject->id); + + $this->assertEquals('FakeCreatePostPoint', $post->reputations->first()->name); + } + /** * it only adds unique point reward if property is set on point type *