rennokki / rating
Laravel Eloquent Rating allows you to assign ratings to any model.
Fund package maintenance!
rennokki
Installs: 15 277
Dependents: 0
Suggesters: 0
Security: 0
Stars: 190
Watchers: 6
Forks: 23
Open Issues: 5
Requires
- illuminate/database: ^8.83|^9.0.1
- illuminate/support: ^8.83|^9.0.1
Requires (Dev)
- laravel/legacy-factories: ^1.3
- mockery/mockery: ^1.5
- orchestra/testbench: ^6.28|^7.0
- orchestra/testbench-core: ^6.28|^7.0
- phpunit/phpunit: ^9.5.13
- dev-master
- 3.1.0
- 3.0.1
- 3.0.0
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/github_actions/codecov/codecov-action-4.2.0
- dev-dependabot/github_actions/actions/cache-4.0.2
- dev-dependabot/composer/laravel/legacy-factories-tw-1.4
- dev-dependabot/github_actions/actions/checkout-4
This package is auto-updated.
Last update: 2024-11-08 08:52:24 UTC
README
Laravel Eloquent Rating allows you to assign ratings to any model, just like any other review based on stars.
🤝 Supporting
If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦
🚀 Installation
Install the package:
$ composer require rennokki/rating
Publish the config:
$ php artisan vendor:publish --provider="Rennokki\Rating\RatingServiceProvider" --tag="config"
Publish the migrations:
$ php artisan vendor:publish --provider="Rennokki\Rating\RatingServiceProvider" --tag="migrations"
Preparing the model
To allow a model to rate other models, it should use the CanRate
trait and implement the Rater
contract.
use Rennokki\Rating\Traits\CanRate; use Rennokki\Rating\Contracts\Rater; class User extends Model implements Rater { use CanRate; ... }
The other models that can be rated should use CanBeRated
trait and Rateable
contract.
use Rennokki\Rating\Traits\CanBeRated; use Rennokki\Rating\Contracts\Rateable; class User extends Model implements Rateable { use CanBeRated; ... }
If your model can both rate & be rated by other models, you should use Rate
trait and Rating
contract.
use Rennokki\Rating\Traits\Rate; use Rennokki\Rating\Contracts\Rating; class User extends Model implements Rating { use Rate; // }
🙌 Usage
To rate other models, simply call rate()
method:
$page = Page::find(1); $user->rate($page, 10); $user->hasRated($page); // true $page->averageRating(User::class); // 10.0, as float
As a second argument to the rate()
method, you can pass the rating score. It can either be string, integer or float.
To update a rating, you can call updateRatingFor()
method:
$user->updateRatingFor($page, 9); $page->averageRating(User::class); // 9.00, as float
As you have seen, you can call averageRating()
within models that can be rated. The return value is the average arithmetic value of all ratings as float
.
If we leave the argument empty, we will get 0.00
because no other Page
model has rated the page so far. But since users have rated the page, we will calculate the average only from the User
models, since only they have voted the page, strictly by passing the class name as the argument.
$page = Page::find(1); $user1->rate($page, 10); $user2->rate($page, 6); $page->averageRating(); // 0.00 $page->averageRating(User::class); // 8.00, as float
While in our example, the User
class can both rate and be rated, we can leave the argument empty if we reference to its class:
$user = User::find(1); $user1->rate($user, 10); $user2->rate($user, 6); $user->averageRating(); // 8.00, as float $user->averageRating(User::class); // 8.00, it is equivalent
The relationships are based on this too:
$page->raters()->get(); // Pages that have rated this page $page->raters(User::class)->get(); // Users that have rated this page $user->ratings()->get(); // Users that this user has rated $user->ratings(Page::class)->get(); // Pages that this user has rated
🐛 Testing
vendor/bin/phpunit
🤝 Contributing
Please see CONTRIBUTING for details.
🔒 Security
If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.