PHPackages                             ghanem/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. ghanem/rating

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

ghanem/rating
=============

Rating system for Laravel

V12.0(2mo ago)8615.5k36[5 issues](https://github.com/AbdullahGhanem/rating/issues)MITPHPPHP ^8.0CI failing

Since Oct 24Pushed 2mo ago7 watchersCompare

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

READMEChangelog (6)Dependencies (5)Versions (9)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/76802a6bb5a95db22a9b9603c3859fc409273d58883165b5de6f2a8881e8abea/68747470733a2f2f706f7365722e707567782e6f72672f6768616e656d2f726174696e672f762f737461626c652e737667)](https://packagist.org/packages/ghanem/rating) [![License](https://camo.githubusercontent.com/807ed4a7999ec6a6904d1083f706c5b0f4de3f9393917c688a5976e1cfc0f497/68747470733a2f2f706f7365722e707567782e6f72672f6768616e656d2f726174696e672f6c6963656e73652e737667)](https://packagist.org/packages/ghanem/rating) [![Total Downloads](https://camo.githubusercontent.com/04b55bc924778f62382358579142bff1d1ca6cc414eaf32ce2c97b3ae42ed082/68747470733a2f2f706f7365722e707567782e6f72672f6768616e656d2f726174696e672f646f776e6c6f6164732e737667)](https://packagist.org/packages/ghanem/rating)

Laravel Rating
==============

[](#laravel-rating)

Rating system for Laravel 8, 9, 10, 11 &amp; 12.

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

[](#installation)

```
composer require ghanem/rating
```

The package uses Laravel's auto-discovery, so no need to manually register the service provider.

Getting started
---------------

[](#getting-started)

Publish and run the migration:

```
php artisan vendor:publish --provider="Ghanem\Rating\RatingServiceProvider"
php artisan migrate
```

Optionally publish the config file:

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

Usage
-----

[](#usage)

### Setup a Model

[](#setup-a-model)

Add the `Ratingable` trait to any model you want to be ratable:

```
use Ghanem\Rating\Traits\Ratingable;

class Post extends Model
{
    use Ratingable;
}
```

Add the `CanRate` trait to the author model:

```
use Ghanem\Rating\Traits\CanRate;

class User extends Model
{
    use CanRate;
}
```

### Create a rating

[](#create-a-rating)

```
// From the ratable model
$rating = $post->rating(['rating' => 5], $user);

// From the author model
$rating = $user->rate($post, ['rating' => 5]);
```

### Create or update a unique rating

[](#create-or-update-a-unique-rating)

Only one rating per author per model:

```
$rating = $post->ratingUnique(['rating' => 5], $user);

// Or from the author
$rating = $user->rateUnique($post, ['rating' => 5]);
```

### Update a rating

[](#update-a-rating)

```
$rating = $post->updateRating($ratingId, ['rating' => 3]);
```

### Delete a rating

[](#delete-a-rating)

```
$post->deleteRating($ratingId);
```

### Rating with review body

[](#rating-with-review-body)

```
$post->rating([
    'rating' => 5,
    'body' => 'Great article!',
], $user);
```

### Scoped ratings (rate different aspects)

[](#scoped-ratings-rate-different-aspects)

```
$restaurant->rating(['rating' => 5, 'type' => 'food'], $user);
$restaurant->rating(['rating' => 3, 'type' => 'service'], $user);

$restaurant->avgRating('food');    // 5.0
$restaurant->avgRating('service'); // 3.0
$restaurant->avgRating();          // 4.0 (all types)
```

### Weighted ratings

[](#weighted-ratings)

```
$post->rating(['rating' => 5, 'weight' => 2], $verifiedUser);
$post->rating(['rating' => 3, 'weight' => 1], $regularUser);

$post->weightedAvgRating(); // 4.33
```

### Aggregates

[](#aggregates)

All aggregate methods accept an optional `$type` parameter for scoped ratings:

```
$post->avgRating()          // average rating
$post->sumRating()          // sum of all ratings
$post->countRatings()       // total count
$post->countPositive()      // count where rating > 0
$post->countNegative()      // count where rating < 0
$post->ratingPercent()      // percentage (default max: 5)
$post->ratingPercent(10)    // percentage with custom max
$post->weightedAvgRating()  // weighted average
```

All available as attributes too:

```
$post->avgRating
$post->sumRating
$post->countRatings
$post->countPositive
$post->countNegative
$post->ratingPercent
$post->weightedAvgRating
```

### Author queries (CanRate)

[](#author-queries-canrate)

```
$user->hasRated($post);          // bool
$user->getRating($post);         // Rating|null
$user->averageGivenRating();     // float
$user->totalGivenRatings();      // int
$user->ratings;                  // all ratings given
```

### Check if rated

[](#check-if-rated)

```
$post->isRatedBy($user);            // bool
$post->isRatedBy($user, 'food');    // bool (scoped)
```

### Query scopes

[](#query-scopes)

```
// Eager load rating aggregates
Post::withAvgRating()->get();
Post::withSumRating()->get();
Post::withCountRatings()->get();

// Order by ratings
Post::orderByAvgRating()->get();        // desc by default
Post::orderByAvgRating('asc')->get();
Post::orderBySumRating()->get();
Post::orderByCountRatings()->get();

// Filter by minimum rating
Post::minAvgRating(3.5)->get();
Post::minSumRating(10)->get();

// Scoped by type
Post::withAvgRating('food')->get();
Post::orderByAvgRating('desc', 'food')->get();
```

Validation
----------

[](#validation)

Configure rating bounds in `config/rating.php`:

```
return [
    'min' => 1,
    'max' => 5,
    'allow_negative' => false,
];
```

Invalid ratings throw `Ghanem\Rating\Exceptions\InvalidRatingException`.

Events
------

[](#events)

The package fires events on rating lifecycle:

- `Ghanem\Rating\Events\RatingCreated`
- `Ghanem\Rating\Events\RatingUpdated`
- `Ghanem\Rating\Events\RatingDeleted`

Each event has a public `$rating` property with the Rating model.

Testing
-------

[](#testing)

```
composer test
```

Sponsor
-------

[](#sponsor)

[Become a Sponsor](https://github.com/sponsors/AbdullahGhanem)

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance84

Actively maintained with recent releases

Popularity38

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 78.6% 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 ~541 days

Recently: every ~819 days

Total

8

Last Release

71d ago

Major Versions

1.4 → V2.012020-12-15

V2.01 → V12.02026-03-08

PHP version history (3 changes)1.0PHP &gt;=5.5.9

V2.01PHP ^7.4|^8.0

V12.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![AbdullahGhanem](https://avatars.githubusercontent.com/u/5055892?v=4)](https://github.com/AbdullahGhanem "AbdullahGhanem (11 commits)")[![cancerimex](https://avatars.githubusercontent.com/u/3083234?v=4)](https://github.com/cancerimex "cancerimex (2 commits)")[![davericher](https://avatars.githubusercontent.com/u/6945951?v=4)](https://github.com/davericher "davericher (1 commits)")

---

Tags

laravellaravelratingratingrating-starslaravelreviewstarsRatingRatable

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[codebyray/laravel-review-rateable

Review &amp; Rating system for Laravel 10, 11 &amp; 12

310351.9k](/packages/codebyray-laravel-review-rateable)[willvincent/laravel-rateable

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

416452.0k3](/packages/willvincent-laravel-rateable)[trexology/reviewrateable

Rating syetem for Laravel 5

3514.4k](/packages/trexology-reviewrateable)[glhd/special

1929.4k](/packages/glhd-special)[bjuppa/laravel-blog

Add blog functionality to your Laravel project

483.3k2](/packages/bjuppa-laravel-blog)

PHPackages © 2026

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