dorvidas/laravel-ratings
Composer 安装命令:
composer require dorvidas/laravel-ratings
包简介
Laravel package for rating users, posts or other models with ease. You can even give a rating to a user for their role on other models i.e., post author or post illustrator.
README 文档
README
Rate users, posts or other models with ease. You can even give a rating to a user for their role on other models i.e., post author or post illustator.
Installation
Lumen
You can install this package via composer using this command:
composer require dorvidas/laravel-ratings
Include service provider and config into bootstrap/app.php:
$app->register(\Dorvidas\Ratings\RatingsServiceProvider::class); $app->configure('ratings');
Laravel
You can install this package via composer using this command:
composer require dorvidas/laravel-ratings
Add service provider:
Dorvidas\Ratings\RatingsServiceProvider::class,
Publish vendor files:
php artisan vendor:publish --tag=public --force
Usage
Giving a rating
In order to rate a model you need to access [RatingBuilder](#API of rating builder) instance.
This can be done in several ways:
- By newing up instance manually:
$builder = new \Dorvidas\Ratings\RatingBuilder; $builder->model($model)->give(5);
- Via facade
Rate::model($model)->give(5);
- Via
\Dorvidas\Ratings\RateableTraittrait method:
$post->rate()->give(5);
API of rating builder
model($model)- set what you will give rating for. When using via trait this can be ommited, because we already know the model we will rate.give(5)- set the rating. The return value is\Dorvidas\Ratings\Models\Ratingmodel.by($user)- set who is rating model. If skipped authorised user is used.on($model)- this is used to define what a user is rated for. Speaking in Laravel terminology we define on which model we rate a user. Below example of rating a user for post:
$post = Post::first(); $user = User::first(); $user->rate()->on($post)->give(5);
as($role)- this is used to define what was a role of a user. Speaking in Laravel terminology we define which model's column holds the user ID. If omitted it is expected that the column isuser_id. An real life example could be giving different ratings for post author and illustrator:
$author = User::first(); $illustrator = User::skip(1)->first(); $author->rate()->on($post)->as('author_id')->give(5); $illustrator()->rate()->on($post)->as('illustrator_id')->give(5);
Getting a ratings
Ratings are retrieved via \Dorvidas\Ratings\Rating model.
Model ratings can be retrieved by querying ratings relations on a model.
Relations is polymorphic.
Don't forget to add \Dorvidas\Ratings\MOdels\RateableTrait for it to work.
Rating model fields:
model- name of the rated model.model_id- ID of rated model.on_model- this is usually used when we rate a User and want to define what for (on which model) we rate him i.e., on "Post".on_model_id- ID of the model we rate model. If we rate use on "Post" this would be post ID.on_model_column- when rating using on model, with this we define what column defines user id. By default this isuser_id.rated_by- ID of the user who rated.rating- actual rating.
Rating model query scopes:
of- filter ratings of a model. It filter bymodelandmodel_idcolumns.on- filter ratings on specific model. It filter byon_modelandon_model_idcolumns.as- filter ratings by role. It filter byon_model_columncolumn.model- filter bymodelcolumn.modelId- filter bymodel_idcolumn.onModel- filter by modelon_modelcolumn.onModelId- filter byon_model_idcolumn.
Examples
An basic exampe of getting rating list of a "Post":
$post = Post::first(); $ratings = Rating::of($post)->get();
If there is no model object, you can pass model ID and model type via scopes modelId and model:
$postId = 1; $ratings = Rating::model(Post::class)->modelId($modelId)->get(); //Or doing where statements $ratings = Rating::where('model', Post::class)->where('model_id, $postId)->get();
If we want to get ratings of a user on specific model:
$user = User::first(); $post = Post::first(); $user->ratings()->on($post)->first(); //Returns Rating model instance $user->ratings()->on($post)->first()->rating; //Returns actual rating
If we want to get ratings of a user on specific model for a specific role:
$user = User::first(); $post = Post::first(); $user->ratings()->on($post)->as('author_id')->first();
Getting rating aggregates
Rating aggregates are stored in table rating_aggregates.
If class is using trait RateableTrait you can get aggregates by querying polymorphic relationship:
$model->rating_aggregates;
Whenever a rating is created an \Dorvidas\Ratings\Events\RatingCreatedEvent event is thrown.
There is also a listener \Dorvidas\Ratings\Listeners\RecalculateRatingAggregatesListener which updates aggregate entry.
So don't forget to register those events and listener.
Config
database_prefix- allows to add database prefix.
dorvidas/laravel-ratings 适用场景与选型建议
dorvidas/laravel-ratings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 137 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Rating」 「Rate」 「vote」 「rateable」 「fivestar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dorvidas/laravel-ratings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dorvidas/laravel-ratings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dorvidas/laravel-ratings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Extension integrates a credit check and more made by the LEONEX Risk Management Platform into your order process. Extensive configuration and evaluation options is provided in the Platform
A Laravel Pulse card to track users & URL's that are being throttled by your Rate Limiters
A/B Testing of content elements
Rating syetem for Laravel 5
Request rate limit
EMS Company Star Rating plugin
统计信息
- 总下载量: 137
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-03