cjmellor/rating
Composer 安装命令:
composer require cjmellor/rating
包简介
A Laravel package that allows for ratings to be added to a Model
README 文档
README
Imagine you have a Recipes Model and you want to have your Users' rate your culinary delights. This package allows you to achieve this.
Example
Add a CanBeRated trait to the Model you want to be ratable.
use Cjmellor\Rating\Concerns\CanBeRated; class Recipe extends Model { use CanBeRated; // ... }
Now you can rate any Model.
$recipe = Recipe::find(1); $recipe->rate(4);
Installation
You can install the package via composer:
composer require cjmellor/rating
You can publish and run the migrations with:
php artisan vendor:publish --tag="rating-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="rating-config"
This is the contents of the published config file:
return [ 'users' => [ 'table' => 'users', 'primary_key' => 'user_id', ], 'max_rating' => 5, 'undo_rating' => true, ];
Usage
To rate a Model, you must add the CanBeRated trait to the Model in question.
use Cjmellor\Rating\Concerns\CanBeRated; class Recipe extends Model { use CanBeRated; // ... }
Rate the Model
$recipe = Recipe::find(1); $recipe->rate(score: 2);
View Models' ratings
$recipe->ratings;
You can get an overall percentage of the amount of Users' who have rated a Model:
Imagine you want a five-star rating system, and you have a Model that has been rated a 3 and a 5 by two Users'
$recipe->ratingPercent(maxLength: 5); // 80.0
This will equate to 80%. A float is returned. Changing the maxLength will recalculate the percentage.
You could then use this percentage for the score attribute of the component.
Note
By default, the
maxLengthis determined by a config option. You can override this by passing a value to the method.
Unrating Models
By default, you can unrate a Model. If you don't want Users' to unrate Models, set the undo_rating config option to true.
To unrate a Model, you can use the unrate method:
$recipe->unrate();
The package comes with a bunch of Attributes that you can use. The results of these are based off a single Model been rated by two Users' with a 3 and 5 rating.
$recipe->averageRating; // "4.0000" $recipe->averageRatingByUser; // "5.0000" $recipe->averageSumOfUser; // 5 $recipe->ratedByUsers; // 2 $recipe->ratedInTotal; // 2 $recipe->sumRating; // "8"
Livewire Component
To see the ratings in action, you can use the Livewire component. This allows you to show the ratings on the front-end statically and let the User's rate the Model by clicking on the stars.
Warning
You must have both Tailwind CSS and Font Awesome installed, though Font Awesome can be replaced with your own preferred icon set
Use the component
<livewire:rating size="text-7xl" score="55" :model="$recipe" />
The component has customisable attributes, including:
public string $iconBgColor = 'text-yellow-300'; public string $iconFgColor = 'text-yellow-400'; public string $iconBg = 'far fa-star'; public string $iconFg = 'fas fa-star'; public float $score = 0; public string $size = 'text-base'; public bool $static = false;
If you have the config for unrating a Model set to true, an icon shows that allows you to unrate the Model.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT Licence (MIT). Please see Licence File for more information.
cjmellor/rating 适用场景与选型建议
cjmellor/rating 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 129 次下载、GitHub Stars 达 35, 最近一次更新时间为 2022 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Rating」 「cjmellor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cjmellor/rating 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cjmellor/rating 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cjmellor/rating 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package allows users to gain experience points (XP) and progress through levels by performing actions on your site. It can provide a simple way to track user progress and implement gamification elements into your application
Approve or Deny new Model data before it is persisted
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
This package allows users to gain experience points (XP) and progress through levels by performing actions on your site. It can provide a simple way to track user progress and implement gamification elements into your application
This package allows users to gain experience points (XP) and progress through levels by performing actions on your site. It can provide a simple way to track user progress and implement gamification elements into your application
Rating syetem for Laravel 5
统计信息
- 总下载量: 129
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 35
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-24