PHPackages                             cjmellor/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. cjmellor/rating

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cjmellor/rating
===============

A Laravel package that allows for ratings to be added to a Model

v2.2.0(2y ago)351252[2 PRs](https://github.com/cjmellor/rating/pulls)MITPHPPHP ^8.1CI passing

Since Oct 25Pushed 1mo ago2 watchersCompare

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

READMEChangelog (8)Dependencies (7)Versions (12)Used By (0)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a465e152165838f94c1a35290758ca422ebbb978ecc30f4887dd33369e925c5d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636a6d656c6c6f722f726174696e673f636f6c6f723d7267622532383536253230313839253230323438253239266c6162656c3d72656c65617365267374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/cjmellor/rating)[![GitHub Tests Action Status](https://camo.githubusercontent.com/e2e084944e094f08b54c1c6f198c5e15a05417fca7cf3f8d34f78be118337771/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636a6d656c6c6f722f726174696e672f72756e2d706573742e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666f722d7468652d626164676526636f6c6f723d726762253238313334253230323339253230313238253239)](https://github.com/cjmellor/rating/actions?query=workflow%3Arun-tests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/921478b19d7faacff9113021179c5b1054487791b0ec998eb001572b9bd97a1c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636a6d656c6c6f722f726174696e672e7376673f636f6c6f723d7267622532383234392532303131352532303232253239267374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/cjmellor/rating)[![Packagist PHP Version](https://camo.githubusercontent.com/a009b58e8dddeb5990849eab2307c1160e583d400b01543a6428bb5e99f0fc3c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f636a6d656c6c6f722f726174696e672f7068703f636f6c6f723d726762253238313635253230313830253230323532253239266c6f676f3d706870266c6f676f436f6c6f723d726762253238313635253230313830253230323532253239267374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/a009b58e8dddeb5990849eab2307c1160e583d400b01543a6428bb5e99f0fc3c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f636a6d656c6c6f722f726174696e672f7068703f636f6c6f723d726762253238313635253230313830253230323532253239266c6f676f3d706870266c6f676f436f6c6f723d726762253238313635253230313830253230323532253239267374796c653d666f722d7468652d6261646765)[![Laravel Version](https://camo.githubusercontent.com/e6f14aab9b3770e4ffa65f52a65d33b83cf85cb3d0619992e5328f6b6e9750dc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d253545392d7267622832333525323036382532303530293f7374796c653d666f722d7468652d6261646765266c6f676f3d6c61726176656c)](https://camo.githubusercontent.com/e6f14aab9b3770e4ffa65f52a65d33b83cf85cb3d0619992e5328f6b6e9750dc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d253545392d7267622832333525323036382532303530293f7374796c653d666f722d7468652d6261646765266c6f676f3d6c61726176656c)[![Livewire Version](https://camo.githubusercontent.com/402c51d5d1b9632eb1585234577ce9cbbeae07fb417d68887d82480296c17958/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c697665776972652d253545322d72676261283235312c2532303131322c2532303136392c25323031293f7374796c653d666f722d7468652d6261646765266c6f676f3d6c69766577697265266c6162656c436f6c6f723d726762612831352c25323032332c25323034322c2532303129266c6f676f436f6c6f723d72676261283235312c2532303131322c2532303136392c2532303129)](https://camo.githubusercontent.com/402c51d5d1b9632eb1585234577ce9cbbeae07fb417d68887d82480296c17958/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c697665776972652d253545322d72676261283235312c2532303131322c2532303136392c25323031293f7374796c653d666f722d7468652d6261646765266c6f676f3d6c69766577697265266c6162656c436f6c6f723d726762612831352c25323032332c25323034322c2532303129266c6f676f436f6c6f723d72676261283235312c2532303131322c2532303136392c2532303129)

Imagine you have a Recipes Model and you want to have your Users' rate your culinary delights. This package allows you to achieve this.

[![](https://camo.githubusercontent.com/a359fc6b4e8ebce97aff7607034f58a40cd3ead3ee1bf2337da873824f2bbf6d/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f526174696e672e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d636a6d656c6c6f72253246726174696e67267061747465726e3d736c616e7465645374617273267374796c653d7374796c655f32266465736372697074696f6e3d526174652b796f75722b4d6f64656c73266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d73746172267769647468733d34303026686569676874733d343030)](https://camo.githubusercontent.com/a359fc6b4e8ebce97aff7607034f58a40cd3ead3ee1bf2337da873824f2bbf6d/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f526174696e672e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d636a6d656c6c6f72253246726174696e67267061747465726e3d736c616e7465645374617273267374796c653d7374796c655f32266465736372697074696f6e3d526174652b796f75722b4d6f64656c73266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d73746172267769647468733d34303026686569676874733d343030)

### Example

[](#example)

Add a `CanBeRated` trait to the Model you want to be ratable.

```
use Cjmellor\Rating\Concerns\CanBeRated;

class Recipe extends Model
{
    use CanBeRated;

    // ...
}
```

Now you can rate any Model.

```
$recipe = Recipe::find(1);

$recipe->rate(4);
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require cjmellor/rating
```

You can publish and run the migrations with:

```
php artisan vendor:publish --tag="rating-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --tag="rating-config"
```

This is the contents of the published config file:

```
return [
    'users' => [
        'table' => 'users',
        'primary_key' => 'user_id',
    ],

    'max_rating' => 5,

    'undo_rating' => true,
];
```

Usage
-----

[](#usage)

To rate a Model, you must add the `CanBeRated` trait to the Model in question.

```
use Cjmellor\Rating\Concerns\CanBeRated;

class Recipe extends Model
{
    use CanBeRated;

    // ...
}
```

**Rate the Model**

```
$recipe = Recipe::find(1);

$recipe->rate(score: 2);
```

**View Models' ratings**

```
$recipe->ratings;
```

You can get an overall percentage of the amount of Users' who have rated a Model:

Imagine you want a five-star rating system, and you have a Model that has been rated a `3` and a `5` by two Users'

```
$recipe->ratingPercent(maxLength: 5);

// 80.0
```

This will equate to 80%. A float is returned. Changing the `maxLength` will recalculate the percentage.

You could then use this percentage for the `score` attribute of the component.

> **Note**
>
> By default, the `maxLength` is determined by a config option. You can override this by passing a value to the method.

**Unrating Models**

By default, you can unrate a Model. If you don't want Users' to unrate Models, set the `undo_rating` config option to true.

To unrate a Model, you can use the `unrate` method:

```
$recipe->unrate();
```

The package comes with a bunch of Attributes that you can use. *The results of these are based off a single Model been rated by two Users' with a `3` and ` 5` rating.*

```
$recipe->averageRating; // "4.0000"
$recipe->averageRatingByUser; // "5.0000"
$recipe->averageSumOfUser; // 5
$recipe->ratedByUsers; // 2
$recipe->ratedInTotal; // 2
$recipe->sumRating; // "8"
```

### Livewire Component

[](#livewire-component)

To see the ratings in action, you can use the Livewire component. This allows you to show the ratings on the front-end statically and let the User's rate the Model by clicking on the stars.

> **Warning**
>
> You must have both Tailwind CSS and Font Awesome installed, though Font Awesome can be replaced with your own preferred icon set

**Use the component**

```

```

The component has customisable attributes, including:

```
public string $iconBgColor = 'text-yellow-300';
public string $iconFgColor = 'text-yellow-400';
public string $iconBg = 'far fa-star';
public string $iconFg = 'fas fa-star';
public float $score = 0;
public string $size = 'text-base';
public bool $static = false;
```

If you have the config for unrating a Model set to `true`, an icon shows that allows you to unrate the Model.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

License
-------

[](#license)

The MIT Licence (MIT). Please see [Licence File](LICENSE.md) for more information.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance60

Regular maintenance activity

Popularity21

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 61.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Recently: every ~56 days

Total

8

Last Release

800d ago

Major Versions

v0.0.1 → v1.0.02022-10-25

v1.0.1 → v2.0.02023-07-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/a50ec73c5a205880cbeeab7dc86f12acbe34eace997d01772d9380a1b2455390?d=identicon)[cmellor](/maintainers/cmellor)

---

Top Contributors

[![cjmellor](https://avatars.githubusercontent.com/u/1848476?v=4)](https://github.com/cjmellor "cjmellor (55 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (20 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (10 commits)")[![514sid](https://avatars.githubusercontent.com/u/140138716?v=4)](https://github.com/514sid "514sid (5 commits)")

---

Tags

laravelratingrating-systemlaravelRatingcjmellor

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/cjmellor-rating/health.svg)

```
[![Health](https://phpackages.com/badges/cjmellor-rating/health.svg)](https://phpackages.com/packages/cjmellor-rating)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[willvincent/laravel-rateable

Allows multiple models to be rated with a fivestar like system.

416452.0k3](/packages/willvincent-laravel-rateable)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[cjmellor/level-up

This package allows users to gain experience points (XP) and progress through levels by performing actions on your site. It can provide a simple way to track user progress and implement gamification elements into your application

66188.9k](/packages/cjmellor-level-up)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
