From d74da1eba72bbc50b293c2d8ad201e6b2f08109c Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Sun, 23 Apr 2017 10:34:02 +0200 Subject: [PATCH 1/5] Fixed model functions --- src/Models/Rating.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Models/Rating.php b/src/Models/Rating.php index af9fa55..31381a3 100644 --- a/src/Models/Rating.php +++ b/src/Models/Rating.php @@ -39,4 +39,40 @@ public function author() * * @return static */ -} + public function createRating(Model $ratingable, $data, Model $author) + { + $rating = new static(); + $rating->fill(array_merge($data, [ + 'author_id' => $author->id, + 'author_type' => get_class($author), + ])); + + $ratingable->ratings()->save($rating); + + return $rating; + } + + /** + * @param $id + * @param $data + * + * @return mixed + */ + public function updateRating($id, $data) + { + $rating = static::find($id); + $rating->update($data); + + return $rating; + } + + /** + * @param $id + * + * @return mixed + */ + public function deleteRating($id) + { + return static::find($id)->delete(); + } +} \ No newline at end of file From 72ddea3d0f58b56799c3ea46d605d59bc3a30987 Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Sun, 23 Apr 2017 11:28:47 +0200 Subject: [PATCH 2/5] Add functions --- src/Traits/Ratingable.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Traits/Ratingable.php b/src/Traits/Ratingable.php index 4623164..88eca84 100644 --- a/src/Traits/Ratingable.php +++ b/src/Traits/Ratingable.php @@ -45,6 +45,25 @@ public function ratingPercent($max = 5) $total = $this->sumRating(); return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0; } + + /** + * + * @return mix + */ + public function countPositive() + { + return $this->ratings()->where('rating', '>', '0')->count(); + } + + /** + * + * @return mix + */ + public function countNegative() + { + $quantity = $this->ratings('rating', '<', '0')->count(); + return ("-$quantity"); + } /** * @param $data @@ -94,4 +113,14 @@ public function getSumRatingAttribute() { return $this->sumRating(); } + + public function getCountPositiveAttribute() + { + return $this->countPositive(); + } + + public function getCountNegativeAttribute() + { + return $this->countNegative(); + } } From 45fcce3a499fcc6cbedc3f6d4ff4ec7c3ec800ed Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Sun, 23 Apr 2017 11:38:09 +0200 Subject: [PATCH 3/5] Fixed count negative && Update README --- src/Traits/Ratingable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Ratingable.php b/src/Traits/Ratingable.php index 88eca84..07be77a 100644 --- a/src/Traits/Ratingable.php +++ b/src/Traits/Ratingable.php @@ -61,7 +61,7 @@ public function countPositive() */ public function countNegative() { - $quantity = $this->ratings('rating', '<', '0')->count(); + $quantity = $this->ratings()->where('rating', '<', '0')->count(); return ("-$quantity"); } From 472842fc222ef34350c50c1502f18ac311ffd7ec Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Sun, 23 Apr 2017 11:38:26 +0200 Subject: [PATCH 4/5] Fixed count negative && Update README --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 8a63d81..1d65e41 100644 --- a/README.md +++ b/README.md @@ -105,3 +105,17 @@ $post->ratingPercent(10)); // Ten star rating system // Note: The value passed in is treated as the maximum allowed value. // This defaults to 5 so it can be called without passing a value as well. ```` + +### Count positive rating: +````php +$post->countPositive + +// $post->countPositive() also works for this. +```` + +### Count negative rating: +````php +$post->countNegative + +// $post->countNegative() also works for this. +```` From ff8782d1fa48d7c8c3c037a0aacb0bf257b0212d Mon Sep 17 00:00:00 2001 From: Ugo Mignon Date: Sun, 23 Apr 2017 12:04:33 +0200 Subject: [PATCH 5/5] Add function update or create Unique rating --- README.md | 12 ++++++++++++ src/Models/Rating.php | 20 ++++++++++++++++++++ src/Traits/Ratingable.php | 14 +++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d65e41..9522d41 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,18 @@ $rating = $post->rating([ dd($rating); ``` +### Create or update a unique rating +```php +$user = User::first(); +$post = Post::first(); + +$rating = $post->ratingUnique([ + 'rating' => 5 +], $user); + +dd($rating); +``` + ### Update a rating ```php $rating = $post->updateRating(1, [ diff --git a/src/Models/Rating.php b/src/Models/Rating.php index 31381a3..96f36c9 100644 --- a/src/Models/Rating.php +++ b/src/Models/Rating.php @@ -52,6 +52,26 @@ public function createRating(Model $ratingable, $data, Model $author) return $rating; } + /** + * @param Model $ratingable + * @param $data + * @param Model $author + * + * @return static + */ + public function createUniqueRating(Model $ratingable, $data, Model $author) + { + $rating = [ + 'author_id' => $author->id, + 'author_type' => get_class($author), + "ratingable_id" => $ratingable->id, + "ratingable_type" => get_class($ratingable), + ]; + + Rating::updateOrCreate($rating, $data); + return $rating; + } + /** * @param $id * @param $data diff --git a/src/Traits/Ratingable.php b/src/Traits/Ratingable.php index 07be77a..bd119f5 100644 --- a/src/Traits/Ratingable.php +++ b/src/Traits/Ratingable.php @@ -77,6 +77,18 @@ public function rating($data, Model $author, Model $parent = null) return (new Rating())->createRating($this, $data, $author); } + /** + * @param $data + * @param Model $author + * @param Model|null $parent + * + * @return static + */ + public function ratingUnique($data, Model $author, Model $parent = null) + { + return (new Rating())->createUniqueRating($this, $data, $author); + } + /** * @param $id * @param $data @@ -118,7 +130,7 @@ public function getCountPositiveAttribute() { return $this->countPositive(); } - + public function getCountNegativeAttribute() { return $this->countNegative();