|
1 | 1 | # Laravel Review Rateable |
2 | 2 | <img alt="Packagist Downloads" src="https://img.shields.io/packagist/dt/codebyray/laravel-review-rateable"> <img alt="GitHub" src="https://img.shields.io/github/license/codebyray/laravel-review-rateable"> <img alt="GitHub release (latest SemVer)" src="https://img.shields.io/github/v/release/codebyray/laravel-review-rateable"> <img alt="TravisCI" src="https://api.travis-ci.com/codebyray/laravel-review-rateable.svg?branch=master"> |
3 | 3 |
|
4 | | -Review Rateable system for laravel 6, 7, 8 & 9. You can rate your models by: |
5 | | -- Overall Rating |
6 | | -- Customer Service Rating |
7 | | -- Quality Rating |
8 | | -- Friendly Rating |
9 | | -- Price Rating |
| 4 | +NOTE: This is a complete rewrite from v1. It is not compatible with previous versions of this package. |
10 | 5 |
|
11 | | -You can also set whether the model being rated is recommended. |
| 6 | + |
| 7 | +Laravel Review Ratable is a flexible package that enables you to attach reviews (with multiple ratings) to any Eloquent model in your Laravel application. The package supports multiple departments, configurable rating boundaries, review approval, and a decoupled service contract so you can easily integrate, test, and extend the functionality. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Polymorphic Reviews & Ratings:** Attach written reviews along with multiple rating values to any model. |
| 12 | +- **Configurable Settings:** Define custom rating keys, labels, and value boundaries (min/max) via a config file. |
| 13 | +- **Department Support:** Organize ratings by department (e.g. default, sales, support) with their own criteria. |
| 14 | +- **Review Approval:** Set a default approval status for reviews (and override per review if needed). |
| 15 | +- **Flexible Data Retrieval:** Retrieve reviews with or without ratings, filter by approval status, and calculate averages. |
| 16 | +- **Service Contract:** Use a dedicated service that implements a contract for a decoupled, testable API. |
| 17 | + |
| 18 | +## Requirements |
| 19 | + |
| 20 | +- PHP 8.1 or higher |
| 21 | +- Laravel 10, 11, or 12 |
12 | 22 |
|
13 | 23 | ## Installation |
14 | 24 |
|
15 | | -First, pull in the package through Composer. |
| 25 | +### 1. Install via Composer |
| 26 | + |
| 27 | +In your Laravel application's root, require the package via Composer. |
16 | 28 |
|
17 | 29 | ``` |
18 | | -composer require codebyray/laravel-review-rateable |
| 30 | +composer require codebyray/laravel-review-rateable:^2.0 |
19 | 31 | ``` |
20 | 32 |
|
21 | | -And then include the service provider within `app/config/app.php`. Note: If you are running Laravel 5.5+ this will be auto loaded for you. |
| 33 | +### 2. Publish Package Assets |
| 34 | +After installation, publish the package config and migration files: |
22 | 35 |
|
23 | 36 | ```php |
24 | | -'providers' => [ |
25 | | - Codebyray\ReviewRateable\ReviewRateableServiceProvider::class |
26 | | -]; |
| 37 | +php artisan vendor:publish --provider="Codebyray\ReviewRateable\ReviewRateableServiceProvider" --tag=config |
27 | 38 | ``` |
28 | | - |
29 | | -At last you need to publish and run the migration. |
| 39 | +```php |
| 40 | +php artisan vendor:publish --provider="Codebyray\ReviewRateable\ReviewRateableServiceProvider" --tag=migrations |
30 | 41 | ``` |
31 | | -php artisan vendor:publish --provider="Codebyray\ReviewRateable\ReviewRateableServiceProvider" --tag="migrations" |
| 42 | +Run the migrations to create the necessary database tables: |
| 43 | +```php |
| 44 | +php artisan migrate |
32 | 45 | ``` |
33 | 46 |
|
34 | | -Run the migration |
35 | | -``` |
36 | | -php artisan migrate |
| 47 | +## Configuration |
| 48 | + |
| 49 | +The package configuration file is located at config/laravel-review-ratable.php. Here you can adjust global settings such as: |
| 50 | + |
| 51 | +- Rating Value Boundaries: |
| 52 | + - min_rating_value: Minimum rating value. |
| 53 | + - max_rating_value: Maximum rating value. |
| 54 | +- Review Approval: |
| 55 | + - review_approved: Default approval status for new reviews. |
| 56 | +- Departments & Rating Labels: Define multiple departments, each with its own set of rating keys and labels. |
| 57 | + |
| 58 | +### Example configuration: |
| 59 | +```php |
| 60 | +<?php |
| 61 | + |
| 62 | +return [ |
| 63 | + 'min_rating_value' => 1, |
| 64 | + 'max_rating_value' => 10, |
| 65 | + 'review_approved' => false, // Reviews will be unapproved by default |
| 66 | + |
| 67 | + 'departments' => [ |
| 68 | + 'default' => [ |
| 69 | + 'ratings' => [ |
| 70 | + 'overall' => 'Overall Rating', |
| 71 | + 'customer_service' => 'Customer Service Rating', |
| 72 | + 'quality' => 'Quality Rating', |
| 73 | + 'price' => 'Price Rating', |
| 74 | + 'recommendation' => 'Would Recommend?', |
| 75 | + ], |
| 76 | + ], |
| 77 | + 'sales' => [ |
| 78 | + 'ratings' => [ |
| 79 | + 'overall' => 'Overall Rating', |
| 80 | + 'communication' => 'Communication Rating', |
| 81 | + 'follow_up' => 'Follow-Up Rating', |
| 82 | + 'recommendation' => 'Would Recommend?', |
| 83 | + ], |
| 84 | + ], |
| 85 | + 'support' => [ |
| 86 | + 'ratings' => [ |
| 87 | + 'overall' => 'Overall Rating', |
| 88 | + 'speed' => 'Response Speed', |
| 89 | + 'knowledge' => 'Knowledge Rating', |
| 90 | + 'recommendation' => 'Would Recommend?', |
| 91 | + ], |
| 92 | + ], |
| 93 | + ], |
| 94 | +]; |
37 | 95 | ``` |
38 | 96 |
|
39 | 97 | ----- |
40 | 98 |
|
41 | | -### Setup a Model |
| 99 | +## Usage |
| 100 | +### Making a Model Reviewable |
| 101 | +To allow a model to be reviewed, add the ``ReviewRatable`` trait to your model. For example, in your ``Product`` model: |
42 | 102 | ```php |
43 | 103 | <?php |
44 | 104 |
|
45 | | -namespace App; |
| 105 | +namespace App\Models; |
46 | 106 |
|
47 | | -use Codebyray\ReviewRateable\Contracts\ReviewRateable; |
48 | | -use Codebyray\ReviewRateable\Traits\ReviewRateable as ReviewRateableTrait; |
49 | 107 | use Illuminate\Database\Eloquent\Model; |
| 108 | +use CodeByRay\LaravelReviewRatable\Traits\ReviewRatable; |
50 | 109 |
|
51 | | -class Post extends Model implements ReviewRateable |
| 110 | +class Product extends Model |
52 | 111 | { |
53 | | - use ReviewRateableTrait; |
| 112 | + use ReviewRatable; |
54 | 113 | } |
55 | 114 | ``` |
56 | | - |
57 | | -### Create a rating |
58 | | -When creating a rating you can specify whether the rating is approved or not by adding approved to the array. This is optional and if left |
59 | | -out the default is not approved to allow for review before posting. |
| 115 | +### Adding a Review |
| 116 | +You can add a review (with ratings) directly via the trait: |
60 | 117 | ```php |
61 | | -$user = User::first(); |
62 | | -$post = Post::first(); |
63 | | - |
64 | | -$rating = $post->rating([ |
65 | | - 'title' => 'This is a test title', |
66 | | - 'body' => 'And we will add some shit here', |
67 | | - 'customer_service_rating' => 5, |
68 | | - 'quality_rating' => 5, |
69 | | - 'friendly_rating' => 5, |
70 | | - 'pricing_rating' => 5, |
71 | | - 'rating' => 5, |
72 | | - 'recommend' => 'Yes', |
73 | | - 'approved' => true, // This is optional and defaults to false |
74 | | -], $user); |
75 | | - |
76 | | -dd($rating); |
| 118 | +$product = Product::find(1); |
| 119 | + |
| 120 | +$product->addReview([ |
| 121 | + 'review' => 'Great product! The quality is superb and customer service was excellent.', |
| 122 | + 'department' => 'sales', // Optional, defaults to 'default' |
| 123 | + 'recommend' => true, |
| 124 | + 'approved' => true, // Optionally override default approval (false) by providing 'approved' |
| 125 | + 'ratings' => [ |
| 126 | + 'overall' => 5, |
| 127 | + 'customer_service' => 4, |
| 128 | + 'quality' => 5, |
| 129 | + 'price' => 4, |
| 130 | + ], |
| 131 | +], auth()->id()); |
77 | 132 | ``` |
78 | 133 |
|
79 | 134 | ### Update a rating |
80 | 135 | ```php |
81 | | -$rating = $post->updateRating(1, [ |
82 | | - 'title' => 'new title', |
83 | | - 'body' => 'new body', |
84 | | - 'customer_service_rating' => 1, |
85 | | - 'quality_rating' => 1, |
86 | | - 'friendly_rating' => 3, |
87 | | - 'pricing_rating' => 4, |
88 | | - 'rating' => 4, |
89 | | - 'recommend' => 'No', |
90 | | - 'approved' => true, // This is optional and defaults to false |
91 | | -]); |
| 136 | +// In Progress |
92 | 137 | ``` |
93 | 138 | ### Marking review as approved |
94 | 139 | ```php |
95 | | -$rating = $post->updateRating(1, ['approved' => true]); |
| 140 | +// Inm progress |
96 | 141 | ``` |
97 | 142 | ### Delete a rating: |
98 | 143 | ```php |
99 | | -$post->deleteRating(1); |
| 144 | +// Inm progress |
100 | 145 | ``` |
101 | 146 |
|
102 | 147 | ### Fetch approved or not approved reviews/ratings for a particular resource |
103 | 148 | ```php |
104 | | -// Get approved ratings |
105 | | -$ratings = $post->getApprovedRatings($post->id, 'desc'); |
106 | | - |
107 | | -// Get not approved ratings |
108 | | -$ratings = $post->getNotApprovedRatings($post->id, 'desc'); |
109 | | - |
110 | | -// Get all ratings whether approved or not |
111 | | -$ratings = $post->getAllRatings($post->id, 'desc'); |
112 | | - |
113 | | -// Get the most recent ratings (limit and sort are optional) |
114 | | -// Limit default is 5, sort default is desc |
115 | | -$ratings = $post->getRecentRatings($post->id, 5, 'desc'); |
116 | | - |
117 | | -// Get the most recent user ratings (limit and sort are optional) |
118 | | -// Limit default is 5, approved default is true, sort default is desc |
119 | | -$userRatings = $post->getRecentUserRatings($user->id, 5, true, 'desc'); |
120 | | - |
| 149 | +// Inm progress |
121 | 150 | ``` |
122 | 151 | ### Fetch the average rating: |
123 | 152 | ````php |
124 | | -// Get Overall Average Rating |
125 | | -$post->averageRating() |
126 | | - |
127 | | -// Get Customer Service Average Rating |
128 | | -$post->averageCustomerServiceRating() |
129 | | - |
130 | | -// Get Quality Average Rating |
131 | | -$post->averageQualityRating() |
132 | | - |
133 | | -// Get Friendly Average Rating |
134 | | -$post->averageFriendlyRating() |
135 | | - |
136 | | -// Get Price Average Rating |
137 | | -$post->averagePricingRating() |
138 | | - |
| 153 | +// Inm progress |
139 | 154 | ```` |
140 | 155 |
|
141 | 156 | or |
142 | 157 |
|
143 | 158 | ````php |
144 | | -$post->averageRating(2) //round to 2 decimal place |
145 | | -$post->averageRating(null, true) //get only approved average rating |
| 159 | +// Inm progress |
146 | 160 | ```` |
147 | 161 |
|
148 | 162 | ### Get all ratings: |
149 | 163 | ```php |
150 | | -$post = Post::first(); |
151 | | - |
152 | | -$ratings = $post->getAllRatings($post->id); |
| 164 | +// Inm progress |
153 | 165 | ``` |
154 | 166 |
|
155 | 167 | ### Count total rating: |
156 | 168 | ````php |
157 | | -$post->countRating() |
| 169 | +// Inm progress |
158 | 170 | ```` |
159 | 171 |
|
160 | 172 | ### Fetch the rating percentage. |
161 | 173 | This is also how you enforce a maximum rating value. |
162 | 174 | ````php |
163 | | -$post->ratingPercent() |
164 | | - |
165 | | -$post->ratingPercent(10)); // Ten star rating system |
166 | | -// Note: The value passed in is treated as the maximum allowed value. |
167 | | -// This defaults to 5 so it can be called without passing a value as well. |
| 175 | +// Inm progress |
168 | 176 | ```` |
169 | 177 |
|
170 | | -### Note |
171 | | -This is a fork from Trexology's - [Original Code - laravel-reviewRateable |
172 | | -](https://github.com/Trexology/laravel-reviewRateable). |
| 178 | +### Notes |
173 | 179 |
|
174 | | -Please note that this code is not used in the original and is not maintained. |
|
0 commit comments