rennokki/rating
Composer 安装命令:
composer require rennokki/rating
包简介
Laravel Eloquent Rating allows you to assign ratings to any model.
README 文档
README
Laravel Eloquent Rating allows you to assign ratings to any model, just like any other review based on stars.
🤝 Supporting
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. 📦
🚀 Installation
Install the package:
$ composer require rennokki/rating
Publish the config:
$ php artisan vendor:publish --provider="Rennokki\Rating\RatingServiceProvider" --tag="config"
Publish the migrations:
$ php artisan vendor:publish --provider="Rennokki\Rating\RatingServiceProvider" --tag="migrations"
Preparing the model
To allow a model to rate other models, it should use the CanRate trait and implement the Rater contract.
use Rennokki\Rating\Traits\CanRate; use Rennokki\Rating\Contracts\Rater; class User extends Model implements Rater { use CanRate; ... }
The other models that can be rated should use CanBeRated trait and Rateable contract.
use Rennokki\Rating\Traits\CanBeRated; use Rennokki\Rating\Contracts\Rateable; class User extends Model implements Rateable { use CanBeRated; ... }
If your model can both rate & be rated by other models, you should use Rate trait and Rating contract.
use Rennokki\Rating\Traits\Rate; use Rennokki\Rating\Contracts\Rating; class User extends Model implements Rating { use Rate; // }
🙌 Usage
To rate other models, simply call rate() method:
$page = Page::find(1); $user->rate($page, 10); $user->hasRated($page); // true $page->averageRating(User::class); // 10.0, as float
As a second argument to the rate() method, you can pass the rating score. It can either be string, integer or float.
To update a rating, you can call updateRatingFor() method:
$user->updateRatingFor($page, 9); $page->averageRating(User::class); // 9.00, as float
As 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.
If 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.
$page = Page::find(1); $user1->rate($page, 10); $user2->rate($page, 6); $page->averageRating(); // 0.00 $page->averageRating(User::class); // 8.00, as float
While in our example, the User class can both rate and be rated, we can leave the argument empty if we reference to its class:
$user = User::find(1); $user1->rate($user, 10); $user2->rate($user, 6); $user->averageRating(); // 8.00, as float $user->averageRating(User::class); // 8.00, it is equivalent
The relationships are based on this too:
$page->raters()->get(); // Pages that have rated this page $page->raters(User::class)->get(); // Users that have rated this page $user->ratings()->get(); // Users that this user has rated $user->ratings(Page::class)->get(); // Pages that this user has rated
🐛 Testing
vendor/bin/phpunit
🤝 Contributing
Please see CONTRIBUTING for details.
🔒 Security
If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.
🎉 Credits
rennokki/rating 适用场景与选型建议
rennokki/rating 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.05k 次下载、GitHub Stars 达 190, 最近一次更新时间为 2018 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「laravel」 「system」 「Rating」 「eloquent」 「stars」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rennokki/rating 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rennokki/rating 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rennokki/rating 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Laravel 5 - Repositories to the database layer
A full-stack framework system built from Aura library packages.
This package provides an Address Model, Migration and Factory. It also provides the traits HasBillingAddres and HasPublicAddress
Users reviews system
统计信息
- 总下载量: 17.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 190
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-07-23