https://github.com/renoki-co/rating
Laravel Eloquent Rating allows you to assign ratings to any model.
https://github.com/renoki-co/rating
assign-ratings eloquent laravel model php rating
Last synced: 10 months ago
JSON representation
Laravel Eloquent Rating allows you to assign ratings to any model.
- Host: GitHub
- URL: https://github.com/renoki-co/rating
- Owner: renoki-co
- License: apache-2.0
- Created: 2018-07-23T17:54:31.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-04-08T07:43:22.000Z (almost 2 years ago)
- Last Synced: 2025-03-28T19:09:55.546Z (10 months ago)
- Topics: assign-ratings, eloquent, laravel, model, php, rating
- Language: PHP
- Homepage:
- Size: 104 KB
- Stars: 190
- Watchers: 5
- Forks: 24
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Laravel Eloquent Rating
=======================

[](https://codecov.io/gh/renoki-co/rating/branch/master)
[](https://github.styleci.io/repos/141194551)
[](https://packagist.org/packages/rennokki/rating)
[](https://packagist.org/packages/rennokki/rating)
[](https://packagist.org/packages/rennokki/rating)
[](https://packagist.org/packages/rennokki/rating)
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](https://github.com/sponsors/rennokki). 📦**
[
](https://github-content.renoki.org/github-repo/22)
## 🚀 Installation
Install the package:
```bash
$ composer require rennokki/rating
```
Publish the config:
```bash
$ php artisan vendor:publish --provider="Rennokki\Rating\RatingServiceProvider" --tag="config"
```
Publish the migrations:
```bash
$ 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.
```php
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.
```php
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.
```php
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:
```php
$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:
```php
$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.
```php
$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:
```php
$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:
```php
$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
``` bash
vendor/bin/phpunit
```
## 🤝 Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## 🔒 Security
If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.
## 🎉 Credits
- [Alex Renoki](https://github.com/rennokki)
- [All Contributors](../../contributors)