├── LICENSE ├── README.md ├── composer.json ├── database └── migrations │ └── create_ratings_table.php.stub └── src ├── Contracts └── ReviewRateable.php ├── Models └── Rating.php ├── ReviewRateableServiceProvider.php └── Traits └── ReviewRateable.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 PackageBackup 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/trexology/reviewrateable/v/stable)](https://packagist.org/packages/trexology/reviewrateable) 2 | [![Total Downloads](https://poser.pugx.org/trexology/reviewrateable/downloads)](https://packagist.org/packages/trexology/reviewrateable) 3 | [![Latest Unstable Version](https://poser.pugx.org/trexology/reviewrateable/v/unstable)](https://packagist.org/packages/trexology/reviewrateable) [![License](https://poser.pugx.org/trexology/reviewrateable/license)](https://packagist.org/packages/trexology/reviewrateable) 4 | 5 | # Laravel ReviewRateable 6 | ReviewRateable system for laravel 5.* 7 | 8 | ## Installation 9 | 10 | First, pull in the package through Composer. 11 | 12 | ```js 13 | composer require trexology/reviewrateable 14 | ``` 15 | 16 | And then include the service provider within `app/config/app.php`. (Skip this step if you are on Laravel 5.5 or above) 17 | 18 | ```php 19 | 'providers' => [ 20 | Trexology\ReviewRateable\ReviewRateableServiceProvider::class 21 | ]; 22 | ``` 23 | 24 | At last you need to publish and run the migration. 25 | ``` 26 | php artisan vendor:publish --provider="Trexology\ReviewRateable\ReviewRateableServiceProvider" && php artisan migrate 27 | ``` 28 | 29 | ----- 30 | 31 | ### Setup a Model 32 | ```php 33 | rating([ 53 | 'title' => 'Some title', 54 | 'body' => 'Some body', //optional 55 | 'anonymous' => 1, //optional 56 | 'rating' => 5, 57 | ], $user); 58 | 59 | dd($rating); 60 | ``` 61 | 62 | ### Update a rating 63 | ```php 64 | $rating = $post->updateRating(1, [ 65 | 'title' => 'new title', 66 | 'body' => 'new body', //optional 67 | 'anonymous' => 1, //optional 68 | 'rating' => 3, 69 | ]); 70 | ``` 71 | 72 | ### Delete a rating: 73 | ```php 74 | $post->deleteRating(1); 75 | ``` 76 | 77 | ### Fetch the average rating: 78 | ````php 79 | $post->averageRating() 80 | ```` 81 | 82 | or 83 | 84 | ````php 85 | $post->averageRating(2) //round to 2 decimal place 86 | ```` 87 | 88 | ### Count total rating: 89 | ````php 90 | $post->countRating() 91 | ```` 92 | 93 | ### Count rating meta (Count and Avg): 94 | ````php 95 | $post->ratingMeta() 96 | ```` 97 | 98 | or 99 | 100 | ````php 101 | $post->ratingMeta(2) //round to 2 decimal place 102 | ```` 103 | 104 | ### Fetch the rating percentage. 105 | This is also how you enforce a maximum rating value. 106 | ````php 107 | $post->ratingPercent() 108 | 109 | $post->ratingPercent(10)); // Ten star rating system 110 | // Note: The value passed in is treated as the maximum allowed value. 111 | // This defaults to 5 so it can be called without passing a value as well. 112 | ```` 113 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "trexology/reviewrateable", 3 | "description": "Rating syetem for Laravel 5", 4 | "keywords": ["rating", "Ratable", "laravel", "ReviewRateable", "reviewable"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Trex Lim", 9 | "email": "trexlim@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "dev", 13 | "require": { 14 | "php": ">=5.5.9", 15 | "illuminate/support": "~5.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "Trexology\\ReviewRateable\\": "src/" 20 | } 21 | }, 22 | "extra": { 23 | "laravel": { 24 | "providers": [ 25 | "Trexology\\ReviewRateable\\ReviewRateableServiceProvider" 26 | ] 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /database/migrations/create_ratings_table.php.stub: -------------------------------------------------------------------------------- 1 | increments('id')->unsigned(); 12 | $table->double('rating'); 13 | $table->string('title'); 14 | $table->text('body')->nullable(); 15 | $table->boolean('anonymous')->default(false); 16 | $table->morphs('reviewrateable'); 17 | $table->morphs('author'); 18 | $table->softDeletes(); 19 | $table->timestamps(); 20 | }); 21 | } 22 | 23 | public function down() 24 | { 25 | Schema::dropIfExists('ratings'); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Contracts/ReviewRateable.php: -------------------------------------------------------------------------------- 1 | morphTo(); 29 | } 30 | 31 | /** 32 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 33 | */ 34 | public function author() 35 | { 36 | return $this->morphTo('author'); 37 | } 38 | 39 | /** 40 | * @param Model $reviewrateable 41 | * @param $data 42 | * @param Model $author 43 | * 44 | * @return static 45 | */ 46 | public function createRating(Model $reviewrateable, $data, Model $author) 47 | { 48 | $rating = new static(); 49 | $rating->fill(array_merge($data, [ 50 | 'author_id' => $author->id, 51 | 'author_type' => get_class($author), 52 | ])); 53 | 54 | $reviewrateable->ratings()->save($rating); 55 | 56 | return $rating; 57 | } 58 | 59 | /** 60 | * @param $id 61 | * @param $data 62 | * 63 | * @return mixed 64 | */ 65 | public function updateRating($id, $data) 66 | { 67 | $rating = static::find($id); 68 | $rating->update($data); 69 | 70 | return $rating; 71 | } 72 | 73 | /** 74 | * @param $id 75 | * 76 | * @return mixed 77 | */ 78 | public function deleteRating($id) 79 | { 80 | return static::find($id)->delete(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/ReviewRateableServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 18 | __DIR__.'/../database/migrations/create_ratings_table.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_ratings_table.php", 19 | ], 'migrations'); 20 | } 21 | 22 | /** 23 | * Register any application services. 24 | * 25 | * @return void 26 | */ 27 | public function register() 28 | { 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Traits/ReviewRateable.php: -------------------------------------------------------------------------------- 1 | morphMany(Rating::class, 'reviewrateable'); 16 | } 17 | 18 | /** 19 | * 20 | * @return mix 21 | */ 22 | public function averageRating($round= null) 23 | { 24 | if ($round) { 25 | return $this->ratings() 26 | ->selectRaw('ROUND(AVG(rating), '.$round.') as averageReviewRateable') 27 | ->pluck('averageReviewRateable')->first(); 28 | } 29 | 30 | return $this->ratings() 31 | ->selectRaw('AVG(rating) as averageReviewRateable') 32 | ->pluck('averageReviewRateable')->first(); 33 | } 34 | 35 | /** 36 | * 37 | * @return mix 38 | */ 39 | public function countRating() 40 | { 41 | return $this->ratings() 42 | ->selectRaw('count(rating) as countReviewRateable') 43 | ->pluck('countReviewRateable')->first(); 44 | } 45 | 46 | /** 47 | * 48 | * @return mix 49 | */ 50 | public function sumRating() 51 | { 52 | return $this->ratings() 53 | ->selectRaw('SUM(rating) as sumReviewRateable') 54 | ->pluck('sumReviewRateable'); 55 | } 56 | 57 | /** 58 | * @param $max 59 | * 60 | * @return mix 61 | */ 62 | public function ratingPercent($max = 5) 63 | { 64 | $ratings = $this->ratings(); 65 | $quantity = $ratings->count(); 66 | $total = $ratings->selectRaw('SUM(rating) as total')->pluck('total'); 67 | return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0; 68 | } 69 | 70 | /** 71 | * @param $data 72 | * @param Model $author 73 | * @param Model|null $parent 74 | * 75 | * @return static 76 | */ 77 | public function rating($data, Model $author, Model $parent = null) 78 | { 79 | return (new Rating())->createRating($this, $data, $author); 80 | } 81 | 82 | /** 83 | * @param $id 84 | * @param $data 85 | * @param Model|null $parent 86 | * 87 | * @return mixed 88 | */ 89 | public function updateRating($id, $data, Model $parent = null) 90 | { 91 | return (new Rating())->updateRating($id, $data); 92 | } 93 | 94 | /** 95 | * @param $id 96 | * 97 | * @return mixed 98 | */ 99 | public function deleteRating($id) 100 | { 101 | return (new Rating())->deleteRating($id); 102 | } 103 | 104 | /** 105 | * 106 | * @return mixed 107 | */ 108 | public function ratingMeta($round= null) { 109 | return [ 110 | "avg" => $this->averageRating($round), 111 | "count" => $this->countRating(), 112 | ]; 113 | } 114 | } 115 | --------------------------------------------------------------------------------