{"id":33988057,"url":"https://github.com/mtvs/eloquent-aggregate-rating","last_synced_at":"2026-04-20T08:03:58.171Z","repository":{"id":57021686,"uuid":"331620846","full_name":"mtvs/eloquent-aggregate-rating","owner":"mtvs","description":"Automatic rating aggregation for Laravel Eloquent models with reviews. ⭐⭐⭐⭐⭐ (5.0 - x reviews)","archived":false,"fork":false,"pushed_at":"2021-01-24T07:58:35.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-14T17:58:11.322Z","etag":null,"topics":["eloquent","laravel","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mtvs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-21T12:27:56.000Z","updated_at":"2021-11-16T15:37:24.000Z","dependencies_parsed_at":"2022-08-23T12:20:39.917Z","dependency_job_id":null,"html_url":"https://github.com/mtvs/eloquent-aggregate-rating","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/mtvs/eloquent-aggregate-rating","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-aggregate-rating","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-aggregate-rating/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-aggregate-rating/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-aggregate-rating/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtvs","download_url":"https://codeload.github.com/mtvs/eloquent-aggregate-rating/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-aggregate-rating/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32038456,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T00:18:06.643Z","status":"online","status_checked_at":"2026-04-20T02:00:06.527Z","response_time":94,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","laravel","php"],"created_at":"2025-12-13T05:49:35.178Z","updated_at":"2026-04-20T08:03:58.160Z","avatar_url":"https://github.com/mtvs.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Eloquent Aggregate Rating\n\nAutomatic rating aggregation for Laravel Eloquent models with reviews.\n\n[![Build Status](https://travis-ci.com/mtvs/eloquent-aggregate-rating.svg?branch=master)](https://travis-ci.com/mtvs/eloquent-aggregate-rating)\n\nThis package aggregates the ratings' average and count of a model, which is \nreviewed, and updates the model on the occurance of the specified events, e.g.:\nafter saving or deleting a review. So, it facilitates the access to these values\nand eliminates the problem of n+1 queries when retrieving a list of the models\nwith their aggregate-rating values and also the need for subqueries when sorting\nthe models based on their rating.\n\n## Setup\n\nFirst, modify the database table of the model that is reviewed and add two\nnew columns, one to store the ratings' average and another one for the ratings'\ncount, forexample:\n\n```php\n\n\t$table-\u003efloat('rating_average')-\u003enullable();\n\t$table-\u003eunsignedInteger('rating_count')-\u003edefault(0);\n\n```\n\nObviously, there has to be a `rating` column on the reviews table.\n\nThen, there're two traits. One is intended to be used in the model that is \nreviewed. It contains an abstract method that has to be implemented to return\nthe relationship to the review model.\n\n```php\n\nuse AggregateRating\\HasAggregateRating;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Database\\Eloquent\\Relations\\HasMany\n\nClass Item extends Model\n{\n\tuse HasAggregateRating;\n\n\tpublic function aggregateRatingReviews(): HasMany\n\t{\n\t\treturn $this-\u003ereviews();\n\t}\n\n\t// ...\n}\n\n```\n\nThe other is to be used in the review model. It contains an abstract method to\nreturn the relationship to the model that is reviewed.\n\n```php\n\nuse AggregateRating\\HasIndividualRating;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Database\\Eloquent\\Relations\\BelongsTo;\n\nClass Review extends Model\n{\n\tuse HasIndividualRating;\n\n\tpublic function aggregateRatingItem(): BelongsTo\n\t{\n\t\treturn $this-\u003eitem();\n\t}\n\n\t// ...\n}\n\n```\n\n### Triggering Events\n\nThe `aggregateRatingEvents()`, which is on the `HasIndividualRating` trait,\nreturns the events on the review model that trigger the process of the aggregation\nand updating the related model. This method's default definition is as the\nfollowing:\n\n```php\n\n\t/**\n     * Return the model events that require updating the aggregate rating.\n     *\n     * @return array\n     */\n\tprotected static function aggregateRatingEvents()\n\t{ \n\t\treturn [\n\t\t\t'saved', 'deleted',\n\t\t];\n\t}\n\n```\n\nBut you can overwrite it to spicify your custom events:\n\n```php\n\n\t/**\n     * Return the model events that require updating the aggregate rating.\n     *\n     * @return array\n     */\n\tprotected static function aggregateRatingEvents()\n\t{ \n\t\treturn [\n\t\t\t'approved', 'suspended', 'rejected', 'deleted',\n\t\t];\n\t}\n\n```\n\n### Customizing The Column Names\n\nIf you want to choose other names for your columns, set the following \nconstants on your models to the corresponding values.\n\n```php\n\nuse AggregateRating\\HasAggregateRating;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Database\\Eloquent\\Relations\\HasMany\n\nClass Item extends Model\n{\n\tuse HasAggregateRating;\n\n\tconst RATING_AVERAGE = 'rating_average';\n\tconst RATING_COUNT = 'rating_count';\n\n\tpublic function aggregateRatingReviews(): HasMany\n\t{\n\t\treturn $this-\u003ereviews();\n\t}\n\n\t// ...\n}\n\n```\n\n```php\n\nuse AggregateRating\\HasIndividualRating;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Database\\Eloquent\\Relations\\BelongsTo;\n\nClass Review extends Model\n{\n\tuse HasIndividualRating;\n\n\tconst RATING = 'rating';\n\n\tpublic function aggregateRatingItem(): BelongsTo\n\t{\n\t\treturn $this-\u003eitem();\n\t}\n\n\t// ...\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtvs%2Feloquent-aggregate-rating","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtvs%2Feloquent-aggregate-rating","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtvs%2Feloquent-aggregate-rating/lists"}