{"id":20244771,"url":"https://github.com/renoki-co/rating","last_synced_at":"2025-04-04T20:14:19.740Z","repository":{"id":36964593,"uuid":"142049701","full_name":"renoki-co/rating","owner":"renoki-co","description":"Laravel Eloquent Rating allows you to assign ratings to any model.","archived":false,"fork":false,"pushed_at":"2024-04-08T07:43:22.000Z","size":107,"stargazers_count":190,"open_issues_count":6,"forks_count":24,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-28T19:09:55.546Z","etag":null,"topics":["assign-ratings","eloquent","laravel","model","php","rating"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/renoki-co.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"rennokki"}},"created_at":"2018-07-23T17:54:31.000Z","updated_at":"2024-03-15T11:01:56.000Z","dependencies_parsed_at":"2024-06-21T19:08:54.021Z","dependency_job_id":"21f116cd-4f79-474b-b864-7f5c2ef72cb0","html_url":"https://github.com/renoki-co/rating","commit_stats":{"total_commits":107,"total_committers":5,"mean_commits":21.4,"dds":"0.28037383177570097","last_synced_commit":"9c9cdc17f418b4638bc9379840112d74c5a13aa9"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renoki-co%2Frating","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renoki-co%2Frating/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renoki-co%2Frating/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renoki-co%2Frating/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/renoki-co","download_url":"https://codeload.github.com/renoki-co/rating/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247242680,"owners_count":20907134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["assign-ratings","eloquent","laravel","model","php","rating"],"created_at":"2024-11-14T09:17:20.937Z","updated_at":"2025-04-04T20:14:19.720Z","avatar_url":"https://github.com/renoki-co.png","language":"PHP","readme":"Laravel Eloquent Rating\n=======================\n\n![CI](https://github.com/renoki-co/rating/workflows/CI/badge.svg?branch=master)\n[![codecov](https://codecov.io/gh/renoki-co/rating/branch/master/graph/badge.svg)](https://codecov.io/gh/renoki-co/rating/branch/master)\n[![StyleCI](https://github.styleci.io/repos/142049701/shield?branch=master)](https://github.styleci.io/repos/141194551)\n[![Latest Stable Version](https://poser.pugx.org/rennokki/rating/v/stable)](https://packagist.org/packages/rennokki/rating)\n[![Total Downloads](https://poser.pugx.org/rennokki/rating/downloads)](https://packagist.org/packages/rennokki/rating)\n[![Monthly Downloads](https://poser.pugx.org/rennokki/rating/d/monthly)](https://packagist.org/packages/rennokki/rating)\n[![License](https://poser.pugx.org/rennokki/rating/license)](https://packagist.org/packages/rennokki/rating)\n\nLaravel Eloquent Rating allows you to assign ratings to any model, just like any other review based on stars.\n\n## 🤝 Supporting\n\n**If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with [Github Sponsors](https://github.com/sponsors/rennokki). 📦**\n\n[\u003cimg src=\"https://github-content.s3.fr-par.scw.cloud/static/22.jpg\" height=\"210\" width=\"418\" /\u003e](https://github-content.renoki.org/github-repo/22)\n\n## 🚀 Installation\n\nInstall the package:\n\n```bash\n$ composer require rennokki/rating\n```\n\nPublish the config:\n\n```bash\n$ php artisan vendor:publish --provider=\"Rennokki\\Rating\\RatingServiceProvider\" --tag=\"config\"\n```\n\nPublish the migrations:\n\n```bash\n$ php artisan vendor:publish --provider=\"Rennokki\\Rating\\RatingServiceProvider\" --tag=\"migrations\"\n```\n\n## Preparing the model\n\nTo allow a model to rate other models, it should use the `CanRate` trait and implement the  `Rater` contract.\n\n```php\nuse Rennokki\\Rating\\Traits\\CanRate;\nuse Rennokki\\Rating\\Contracts\\Rater;\n\nclass User extends Model implements Rater\n{\n    use CanRate;\n    ...\n}\n```\n\nThe other models that can be rated should use `CanBeRated` trait and `Rateable` contract.\n\n```php\nuse Rennokki\\Rating\\Traits\\CanBeRated;\nuse Rennokki\\Rating\\Contracts\\Rateable;\n\nclass User extends Model implements Rateable\n{\n    use CanBeRated;\n    ...\n}\n```\n\nIf your model can both rate \u0026 be rated by other models, you should use `Rate` trait and `Rating` contract.\n\n```php\nuse Rennokki\\Rating\\Traits\\Rate;\nuse Rennokki\\Rating\\Contracts\\Rating;\n\nclass User extends Model implements Rating\n{\n    use Rate;\n\n    //\n}\n```\n\n## 🙌 Usage\n\nTo rate other models, simply call `rate()` method:\n\n```php\n$page = Page::find(1);\n\n$user-\u003erate($page, 10);\n$user-\u003ehasRated($page); // true\n$page-\u003eaverageRating(User::class); // 10.0, as float\n```\n\nAs a second argument to the `rate()` method, you can pass the rating score. It can either be string, integer or float.\n\nTo update a rating, you can call `updateRatingFor()` method:\n\n```php\n$user-\u003eupdateRatingFor($page, 9);\n$page-\u003eaverageRating(User::class); // 9.00, as float\n```\n\nAs 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`.\n\nIf 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.\n\n```php\n$page = Page::find(1);\n\n$user1-\u003erate($page, 10);\n$user2-\u003erate($page, 6);\n\n$page-\u003eaverageRating(); // 0.00\n$page-\u003eaverageRating(User::class); // 8.00, as float\n```\n\nWhile in our example, the `User` class can both rate and be rated, we can leave the argument empty if we reference to its class:\n\n```php\n$user = User::find(1);\n\n$user1-\u003erate($user, 10);\n$user2-\u003erate($user, 6);\n\n$user-\u003eaverageRating(); // 8.00, as float\n$user-\u003eaverageRating(User::class); // 8.00, it is equivalent\n```\n\nThe relationships are based on this too:\n\n```php\n$page-\u003eraters()-\u003eget(); // Pages that have rated this page\n$page-\u003eraters(User::class)-\u003eget(); // Users that have rated this page\n\n$user-\u003eratings()-\u003eget(); // Users that this user has rated\n$user-\u003eratings(Page::class)-\u003eget(); // Pages that this user has rated\n```\n\n## 🐛 Testing\n\n``` bash\nvendor/bin/phpunit\n```\n\n## 🤝 Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## 🔒  Security\n\nIf you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.\n\n## 🎉 Credits\n\n- [Alex Renoki](https://github.com/rennokki)\n- [All Contributors](../../contributors)\n","funding_links":["https://github.com/sponsors/rennokki"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenoki-co%2Frating","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frenoki-co%2Frating","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenoki-co%2Frating/lists"}