Skip to content

Commit 5dae722

Browse files
Modernize package for Laravel 8-12 with bug fixes and tests
- Fix namespace casing, createUniqueRating return type, countNegative return type - Update composer.json for PHP 8+ and Laravel 8-12 support with auto-discovery - Replace custom artisan command with publishesMigrations() - Modernize migration to anonymous class with $table->id() - Add proper type hints and return types throughout - Add test suite with 13 tests using orchestra/testbench - Add GitHub Actions CI workflow - Update README with modern installation instructions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e09fe27 commit 5dae722

File tree

14 files changed

+9177
-315
lines changed

14 files changed

+9177
-315
lines changed

.github/workflows/tests.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
php: [8.1, 8.2, 8.3, 8.4]
17+
18+
name: PHP ${{ matrix.php }}
19+
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Setup PHP
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ matrix.php }}
27+
extensions: sqlite3
28+
coverage: none
29+
30+
- name: Install dependencies
31+
run: composer install --prefer-dist --no-interaction
32+
33+
- name: Run tests
34+
run: vendor/bin/phpunit

README.md

Lines changed: 59 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,124 @@
1-
[![Latest Stable Version](https://poser.pugx.org/ghanem/rating/v/stable.svg)](https://packagist.org/packages/ghanem/rating) [![License](https://poser.pugx.org/ghanem/rating/license.svg)](https://packagist.org/packages/ghanem/rating)
2-
3-
[![Total Downloads](https://poser.pugx.org/ghanem/rating/downloads.svg)](https://packagist.org/packages/ghanem/rating)
1+
[![Latest Stable Version](https://poser.pugx.org/ghanem/rating/v/stable.svg)](https://packagist.org/packages/ghanem/rating) [![License](https://poser.pugx.org/ghanem/rating/license.svg)](https://packagist.org/packages/ghanem/rating) [![Total Downloads](https://poser.pugx.org/ghanem/rating/downloads.svg)](https://packagist.org/packages/ghanem/rating)
42

53
# Laravel Rating
6-
![https://scontent-cai1-1.xx.fbcdn.net/v/t31.0-8/18192521_1536772739688541_5883708562629992092_o.jpg?oh=281577e64a1e326ff1989f047ab21df6&oe=59BAEBCA](https://scontent-cai1-1.xx.fbcdn.net/v/t31.0-8/18192521_1536772739688541_5883708562629992092_o.jpg?oh=281577e64a1e326ff1989f047ab21df6&oe=59BAEBCA)
7-
Rating system for laravel 5
84

9-
## Installation
5+
Rating system for Laravel 8, 9, 10, 11 & 12.
106

11-
First, pull in the package through Composer.
7+
## Installation
128

13-
```js
9+
```bash
1410
composer require ghanem/rating
1511
```
16-
or add this in your project's composer.json file .
17-
````
18-
"require": {
19-
"Ghanem/Rating": "1.*",
20-
}
21-
````
2212

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.
2414

25-
```php
26-
'providers' => [
27-
Ghanem\Rating\RatingServiceProvider::class
28-
];
29-
```
30-
31-
-----
3215
## Getting started
33-
After the package is correctly installed, you need to generate the migration.
34-
````
35-
php artisan rating:migration
36-
````
3716

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"
4021
php artisan migrate
41-
````
22+
```
4223

43-
After the migration, one new table will be present, `ratings`.
24+
This creates the `ratings` table.
4425

4526
## Usage
27+
4628
### Setup a Model
29+
30+
Add the `Ratingable` trait to any model you want to be ratable:
31+
4732
```php
4833
<?php
4934

50-
namespace App;
35+
namespace App\Models;
5136

52-
use Ghanem\Rating\Traits\Ratingable as Rating;
37+
use Ghanem\Rating\Traits\Ratingable;
5338
use Illuminate\Database\Eloquent\Model;
5439

55-
class Post extends Model implements Rating
40+
class Post extends Model
5641
{
57-
use Rating;
42+
use Ratingable;
5843
}
5944
```
6045

6146
### Create a rating
47+
6248
```php
6349
$user = User::first();
6450
$post = Post::first();
6551

6652
$rating = $post->rating([
6753
'rating' => 5
6854
], $user);
69-
70-
dd($rating);
7155
```
7256

7357
### Create or update a unique rating
74-
```php
75-
$user = User::first();
76-
$post = Post::first();
7758

59+
Only one rating per author per model:
60+
61+
```php
7862
$rating = $post->ratingUnique([
7963
'rating' => 5
8064
], $user);
81-
82-
dd($rating);
8365
```
8466

8567
### Update a rating
68+
8669
```php
87-
$rating = $post->updateRating(1, [
70+
$rating = $post->updateRating($ratingId, [
8871
'rating' => 3
8972
]);
9073
```
9174

92-
### Delete a rating:
75+
### Delete a rating
76+
9377
```php
94-
$post->deleteRating(1);
78+
$post->deleteRating($ratingId);
9579
```
9680

97-
### fetch the Sum rating:
98-
````php
99-
$post->sumRating
81+
### Average rating
10082

101-
// $post->sumRating() also works for this.
102-
````
83+
```php
84+
$post->avgRating // attribute
85+
$post->avgRating() // method
86+
```
10387

104-
### fetch the average rating:
105-
````php
106-
$post->avgRating
88+
### Sum rating
10789

108-
// $post->avgRating() also works for this.
109-
````
90+
```php
91+
$post->sumRating // attribute
92+
$post->sumRating() // method
93+
```
11094

111-
### fetch the rating percentage.
112-
This is also how you enforce a maximum rating value.
113-
````php
114-
$post->ratingPercent
95+
### Rating percentage
11596

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+
```
120101

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+
```
124108

125-
// $post->countPositive() also works for this.
126-
````
109+
### Count negative ratings
127110

128-
### Count negative rating:
129-
````php
130-
$post->countNegative
111+
```php
112+
$post->countNegative // attribute
113+
$post->countNegative() // method
114+
```
131115

132-
// $post->countNegative() also works for this.
133-
````
116+
## Testing
134117

118+
```bash
119+
composer test
120+
```
135121

136122
## Sponsor
137123

138-
[💚️ Become a Sponsor](https://github.com/sponsors/AbdullahGhanem)
124+
[Become a Sponsor](https://github.com/sponsors/AbdullahGhanem)

composer.json

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
{
22
"name": "ghanem/rating",
3-
"description": "Rating syetem for Laravel",
4-
"keywords": ["rating", "Ratable", "laravel"],
3+
"description": "Rating system for Laravel",
4+
"keywords": ["rating", "ratable", "laravel", "stars", "review"],
55
"license": "MIT",
66
"authors": [
77
{
88
"name": "abdullah ghanem",
99
"email": "3bdullah.ghanem@gmail.com"
1010
}
1111
],
12-
"minimum-stability": "dev",
1312
"require": {
14-
"php": "^7.4|^8.0",
15-
"illuminate/support": "^5.0|^7.0|^8.0"
13+
"php": "^8.0",
14+
"illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0",
15+
"illuminate/database": "^8.0|^9.0|^10.0|^11.0|^12.0"
16+
},
17+
"require-dev": {
18+
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0|^10.0",
19+
"phpunit/phpunit": "^9.0|^10.0|^11.0"
1620
},
1721
"autoload": {
1822
"psr-4": {
1923
"Ghanem\\Rating\\": "src/"
2024
}
21-
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Ghanem\\Rating\\Tests\\": "tests/"
29+
}
30+
},
31+
"extra": {
32+
"laravel": {
33+
"providers": [
34+
"Ghanem\\Rating\\RatingServiceProvider"
35+
]
36+
}
37+
},
38+
"scripts": {
39+
"test": "vendor/bin/phpunit"
40+
},
41+
"minimum-stability": "dev",
42+
"prefer-stable": true
2243
}

0 commit comments

Comments
 (0)