|
1 | | -[](https://packagist.org/packages/ghanem/rating) [](https://packagist.org/packages/ghanem/rating) |
2 | | - |
3 | | -[](https://packagist.org/packages/ghanem/rating) |
| 1 | +[](https://packagist.org/packages/ghanem/rating) [](https://packagist.org/packages/ghanem/rating) [](https://packagist.org/packages/ghanem/rating) |
4 | 2 |
|
5 | 3 | # Laravel Rating |
6 | | - |
7 | | -Rating system for laravel 5 |
8 | 4 |
|
9 | | -## Installation |
| 5 | +Rating system for Laravel 8, 9, 10, 11 & 12. |
10 | 6 |
|
11 | | -First, pull in the package through Composer. |
| 7 | +## Installation |
12 | 8 |
|
13 | | -```js |
| 9 | +```bash |
14 | 10 | composer require ghanem/rating |
15 | 11 | ``` |
16 | | -or add this in your project's composer.json file . |
17 | | -```` |
18 | | -"require": { |
19 | | - "Ghanem/Rating": "1.*", |
20 | | -} |
21 | | -```` |
22 | 12 |
|
23 | | -And then include the service provider within `app/config/app.php`. |
| 13 | +The package uses Laravel's auto-discovery, so no need to manually register the service provider. |
24 | 14 |
|
25 | | -```php |
26 | | -'providers' => [ |
27 | | - Ghanem\Rating\RatingServiceProvider::class |
28 | | -]; |
29 | | -``` |
30 | | - |
31 | | ------ |
32 | 15 | ## Getting started |
33 | | -After the package is correctly installed, you need to generate the migration. |
34 | | -```` |
35 | | -php artisan rating:migration |
36 | | -```` |
37 | 16 |
|
38 | | -It will generate the `<timestamp>_create_ratings_table.php` migration. You may now run it with the artisan migrate command: |
39 | | -```` |
| 17 | +Publish and run the migration: |
| 18 | + |
| 19 | +```bash |
| 20 | +php artisan vendor:publish --provider="Ghanem\Rating\RatingServiceProvider" |
40 | 21 | php artisan migrate |
41 | | -```` |
| 22 | +``` |
42 | 23 |
|
43 | | -After the migration, one new table will be present, `ratings`. |
| 24 | +This creates the `ratings` table. |
44 | 25 |
|
45 | 26 | ## Usage |
| 27 | + |
46 | 28 | ### Setup a Model |
| 29 | + |
| 30 | +Add the `Ratingable` trait to any model you want to be ratable: |
| 31 | + |
47 | 32 | ```php |
48 | 33 | <?php |
49 | 34 |
|
50 | | -namespace App; |
| 35 | +namespace App\Models; |
51 | 36 |
|
52 | | -use Ghanem\Rating\Traits\Ratingable as Rating; |
| 37 | +use Ghanem\Rating\Traits\Ratingable; |
53 | 38 | use Illuminate\Database\Eloquent\Model; |
54 | 39 |
|
55 | | -class Post extends Model implements Rating |
| 40 | +class Post extends Model |
56 | 41 | { |
57 | | - use Rating; |
| 42 | + use Ratingable; |
58 | 43 | } |
59 | 44 | ``` |
60 | 45 |
|
61 | 46 | ### Create a rating |
| 47 | + |
62 | 48 | ```php |
63 | 49 | $user = User::first(); |
64 | 50 | $post = Post::first(); |
65 | 51 |
|
66 | 52 | $rating = $post->rating([ |
67 | 53 | 'rating' => 5 |
68 | 54 | ], $user); |
69 | | - |
70 | | -dd($rating); |
71 | 55 | ``` |
72 | 56 |
|
73 | 57 | ### Create or update a unique rating |
74 | | -```php |
75 | | -$user = User::first(); |
76 | | -$post = Post::first(); |
77 | 58 |
|
| 59 | +Only one rating per author per model: |
| 60 | + |
| 61 | +```php |
78 | 62 | $rating = $post->ratingUnique([ |
79 | 63 | 'rating' => 5 |
80 | 64 | ], $user); |
81 | | - |
82 | | -dd($rating); |
83 | 65 | ``` |
84 | 66 |
|
85 | 67 | ### Update a rating |
| 68 | + |
86 | 69 | ```php |
87 | | -$rating = $post->updateRating(1, [ |
| 70 | +$rating = $post->updateRating($ratingId, [ |
88 | 71 | 'rating' => 3 |
89 | 72 | ]); |
90 | 73 | ``` |
91 | 74 |
|
92 | | -### Delete a rating: |
| 75 | +### Delete a rating |
| 76 | + |
93 | 77 | ```php |
94 | | -$post->deleteRating(1); |
| 78 | +$post->deleteRating($ratingId); |
95 | 79 | ``` |
96 | 80 |
|
97 | | -### fetch the Sum rating: |
98 | | -````php |
99 | | -$post->sumRating |
| 81 | +### Average rating |
100 | 82 |
|
101 | | -// $post->sumRating() also works for this. |
102 | | -```` |
| 83 | +```php |
| 84 | +$post->avgRating // attribute |
| 85 | +$post->avgRating() // method |
| 86 | +``` |
103 | 87 |
|
104 | | -### fetch the average rating: |
105 | | -````php |
106 | | -$post->avgRating |
| 88 | +### Sum rating |
107 | 89 |
|
108 | | -// $post->avgRating() also works for this. |
109 | | -```` |
| 90 | +```php |
| 91 | +$post->sumRating // attribute |
| 92 | +$post->sumRating() // method |
| 93 | +``` |
110 | 94 |
|
111 | | -### fetch the rating percentage. |
112 | | -This is also how you enforce a maximum rating value. |
113 | | -````php |
114 | | -$post->ratingPercent |
| 95 | +### Rating percentage |
115 | 96 |
|
116 | | -$post->ratingPercent(10)); // Ten star rating system |
117 | | -// Note: The value passed in is treated as the maximum allowed value. |
118 | | -// This defaults to 5 so it can be called without passing a value as well. |
119 | | -```` |
| 97 | +```php |
| 98 | +$post->ratingPercent // defaults to max of 5 |
| 99 | +$post->ratingPercent(10) // ten star rating system |
| 100 | +``` |
120 | 101 |
|
121 | | -### Count positive rating: |
122 | | -````php |
123 | | -$post->countPositive |
| 102 | +### Count positive ratings |
| 103 | + |
| 104 | +```php |
| 105 | +$post->countPositive // attribute |
| 106 | +$post->countPositive() // method |
| 107 | +``` |
124 | 108 |
|
125 | | -// $post->countPositive() also works for this. |
126 | | -```` |
| 109 | +### Count negative ratings |
127 | 110 |
|
128 | | -### Count negative rating: |
129 | | -````php |
130 | | -$post->countNegative |
| 111 | +```php |
| 112 | +$post->countNegative // attribute |
| 113 | +$post->countNegative() // method |
| 114 | +``` |
131 | 115 |
|
132 | | -// $post->countNegative() also works for this. |
133 | | -```` |
| 116 | +## Testing |
134 | 117 |
|
| 118 | +```bash |
| 119 | +composer test |
| 120 | +``` |
135 | 121 |
|
136 | 122 | ## Sponsor |
137 | 123 |
|
138 | | -[💚️ Become a Sponsor](https://github.com/sponsors/AbdullahGhanem) |
| 124 | +[Become a Sponsor](https://github.com/sponsors/AbdullahGhanem) |
0 commit comments