PHPackages                             mtvs/eloquent-aggregate-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. mtvs/eloquent-aggregate-rating

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

mtvs/eloquent-aggregate-rating
==============================

Automatic rating aggregation for Laravel Eloquent models with reviews

v2.0.0(5y ago)18PHP

Since Jan 21Pushed 5y ago1 watchersCompare

[ Source](https://github.com/mtvs/eloquent-aggregate-rating)[ Packagist](https://packagist.org/packages/mtvs/eloquent-aggregate-rating)[ RSS](/packages/mtvs-eloquent-aggregate-rating/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (4)Used By (0)

Eloquent Aggregate Rating
=========================

[](#eloquent-aggregate-rating)

Automatic rating aggregation for Laravel Eloquent models with reviews.

[![Build Status](https://camo.githubusercontent.com/5648899413fe1ce761c9f5acdb7f7c7a0af3ef85c60ec574c216465a4a8155e2/68747470733a2f2f7472617669732d63692e636f6d2f6d7476732f656c6f7175656e742d6167677265676174652d726174696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/mtvs/eloquent-aggregate-rating)

This package aggregates the ratings' average and count of a model, which is reviewed, and updates the model on the occurance of the specified events, e.g.: after saving or deleting a review. So, it facilitates the access to these values and eliminates the problem of n+1 queries when retrieving a list of the models with their aggregate-rating values and also the need for subqueries when sorting the models based on their rating.

Setup
-----

[](#setup)

First, modify the database table of the model that is reviewed and add two new columns, one to store the ratings' average and another one for the ratings' count, forexample:

```
	$table->float('rating_average')->nullable();
	$table->unsignedInteger('rating_count')->default(0);
```

Obviously, there has to be a `rating` column on the reviews table.

Then, there're two traits. One is intended to be used in the model that is reviewed. It contains an abstract method that has to be implemented to return the relationship to the review model.

```
use AggregateRating\HasAggregateRating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany

Class Item extends Model
{
	use HasAggregateRating;

	public function aggregateRatingReviews(): HasMany
	{
		return $this->reviews();
	}

	// ...
}
```

The other is to be used in the review model. It contains an abstract method to return the relationship to the model that is reviewed.

```
use AggregateRating\HasIndividualRating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Class Review extends Model
{
	use HasIndividualRating;

	public function aggregateRatingItem(): BelongsTo
	{
		return $this->item();
	}

	// ...
}
```

### Triggering Events

[](#triggering-events)

The `aggregateRatingEvents()`, which is on the `HasIndividualRating` trait, returns the events on the review model that trigger the process of the aggregation and updating the related model. This method's default definition is as the following:

```
	/**
     * Return the model events that require updating the aggregate rating.
     *
     * @return array
     */
	protected static function aggregateRatingEvents()
	{
		return [
			'saved', 'deleted',
		];
	}
```

But you can overwrite it to spicify your custom events:

```
	/**
     * Return the model events that require updating the aggregate rating.
     *
     * @return array
     */
	protected static function aggregateRatingEvents()
	{
		return [
			'approved', 'suspended', 'rejected', 'deleted',
		];
	}
```

### Customizing The Column Names

[](#customizing-the-column-names)

If you want to choose other names for your columns, set the following constants on your models to the corresponding values.

```
use AggregateRating\HasAggregateRating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany

Class Item extends Model
{
	use HasAggregateRating;

	const RATING_AVERAGE = 'rating_average';
	const RATING_COUNT = 'rating_count';

	public function aggregateRatingReviews(): HasMany
	{
		return $this->reviews();
	}

	// ...
}
```

```
use AggregateRating\HasIndividualRating;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

Class Review extends Model
{
	use HasIndividualRating;

	const RATING = 'rating';

	public function aggregateRatingItem(): BelongsTo
	{
		return $this->item();
	}

	// ...
}
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~1 days

Total

2

Last Release

1933d ago

### Community

Maintainers

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

---

Top Contributors

[![mtvs](https://avatars.githubusercontent.com/u/8286154?v=4)](https://github.com/mtvs "mtvs (20 commits)")

---

Tags

eloquentlaravelphp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mtvs-eloquent-aggregate-rating/health.svg)

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

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[staudenmeir/eloquent-json-relations

Laravel Eloquent relationships with JSON keys

1.1k5.8M24](/packages/staudenmeir-eloquent-json-relations)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[gearbox-solutions/eloquent-filemaker

A package for getting FileMaker records as Eloquent models in Laravel

6454.8k2](/packages/gearbox-solutions-eloquent-filemaker)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)

PHPackages © 2026

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