diff --git a/README.md b/README.md index 8a63d81..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, [ @@ -105,3 +117,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. +```` diff --git a/src/Models/Rating.php b/src/Models/Rating.php index af9fa55..96f36c9 100644 --- a/src/Models/Rating.php +++ b/src/Models/Rating.php @@ -39,4 +39,60 @@ 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 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 + * + * @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 diff --git a/src/Traits/Ratingable.php b/src/Traits/Ratingable.php index 4623164..bd119f5 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()->where('rating', '<', '0')->count(); + return ("-$quantity"); + } /** * @param $data @@ -58,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 @@ -94,4 +125,14 @@ public function getSumRatingAttribute() { return $this->sumRating(); } + + public function getCountPositiveAttribute() + { + return $this->countPositive(); + } + + public function getCountNegativeAttribute() + { + return $this->countNegative(); + } }