PHPackages                             sojeda/rating - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. sojeda/rating

ActiveLibrary[Database &amp; ORM](/categories/database)

sojeda/rating
=============

Laravel Eloquent Rating allows you to assign ratings to any model.

v2.2.3(5y ago)021MITPHPPHP ^7.4

Since Jul 23Pushed 5y ago1 watchersCompare

[ Source](https://github.com/sojeda/rating)[ Packagist](https://packagist.org/packages/sojeda/rating)[ Docs](https://github.com/sojeda/rating)[ RSS](/packages/sojeda-rating/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (9)Versions (16)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/e5a6e3e4e68922ed689c70f0cca3b6de8582cab438180b7432624f5823c76c10/68747470733a2f2f706f7365722e707567782e6f72672f736f6a6564612f726174696e672f76)](//packagist.org/packages/sojeda/rating)[![Total Downloads](https://camo.githubusercontent.com/40a1d0ea69d92d1a692a92359e8d330f6d553537c885b4b1286e97eb683b8571/68747470733a2f2f706f7365722e707567782e6f72672f736f6a6564612f726174696e672f646f776e6c6f616473)](//packagist.org/packages/sojeda/rating)[![Latest Unstable Version](https://camo.githubusercontent.com/dbda40dde6c04f8731f0e12aa199bbbe2464c18a2f7387ac92a5026985938e92/68747470733a2f2f706f7365722e707567782e6f72672f736f6a6564612f726174696e672f762f756e737461626c65)](//packagist.org/packages/sojeda/rating)[![License](https://camo.githubusercontent.com/8c6806c880484c8b5dba3c2c2bcc57a344de060d18f60f96c0be319721d8bb4b/68747470733a2f2f706f7365722e707567782e6f72672f736f6a6564612f726174696e672f6c6963656e7365)](//packagist.org/packages/sojeda/rating)[![StyleCI](https://camo.githubusercontent.com/6d6251ecde8254d053f83f7c161dbdb47e179cedf055c0af1964113a977f9dc2/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3136393737373139332f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/169777193)[![codecov](https://camo.githubusercontent.com/07fd26a51a32b201be6c1ef3e3e451d20e139e7e1e580e6cec2235c60ddb7f97/68747470733a2f2f636f6465636f762e696f2f67682f736f6a6564612f726174696e672f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/sojeda/rating)

[![PayPal](https://camo.githubusercontent.com/e3d021da65162fda975bd58ef4a4d80bdd03d6e94aa1b321d179e6b4c1aff357/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f50617950616c2d646f6e6174652d626c75652e737667)](https://paypal.me/soj3da)

Laravel Eloquent Rating
=======================

[](#laravel-eloquent-rating)

Laravel Eloquent Rating allows you to assign ratings to any model.

Installation
============

[](#installation)

Install the package:

```
$ composer require sojeda/rating
```

If your Laravel version does not support package discovery, add this line in the `providers` array in your `config/app.php` file:

```
Laraveles\Rating\RatingServiceProvider::class,
```

Publish the config file &amp; migration files:

```
$ php artisan vendor:publish --provider='Laraveles\Rating\RatingServiceProvider'
```

Migrate the database:

```
$ php artisan migrate
```

Preparing the Model
===================

[](#preparing-the-model)

To allow a model to rate other models, it should use the `CanRate` trait and implement the `Qualifier` contract.

```
use Laraveles\Rating\Traits\CanRate;
use Laraveles\Rating\Contracts\Qualifier;

class User extends Model implements Qualifier {
    use CanRate;
    ...
}
```

The other models that can be rated should use `CanBeRated` trait and `Rateable` contract.

```
use Laraveles\Rating\Traits\CanBeRated;
use Laraveles\Rating\Contracts\Rateable;

class User extends Model implements Rateable {
    use CanBeRated;
    ...
}
```

If your model can both rate &amp; be rated by other models, you should use `Rate` trait and `Rating` contract.

```
use Laraveles\Rating\Traits\Rate;
use Laraveles\Rating\Contracts\Rating;

class User extends Model implements Rating {
    use Rate;
    ...
}
```

Usage
=====

[](#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->qualifiers()->get(); // Pages that have rated this page
$page->qualifiers(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
```

Events
------

[](#events)

`ModelRated`

You can define your own listeners in your app's EventServiceProvider. E.g.:

```
