sojeda/rating
Composer 安装命令:
composer require sojeda/rating
包简介
Laravel Eloquent Rating allows you to assign ratings to any model.
README 文档
README
Laravel Eloquent Rating
Laravel Eloquent Rating allows you to assign ratings to any model.
Installation
Install the package:
$ composer require sojeda/rating
If your Laravel version does not support package discovery, add this line in the providers array in your config/app.php file:
Laraveles\Rating\RatingServiceProvider::class,
Publish the config file & migration files:
$ php artisan vendor:publish --provider='Laraveles\Rating\RatingServiceProvider'
Migrate the database:
$ php artisan migrate
Preparing the Model
To allow a model to rate other models, it should use the CanRate trait and implement the Qualifier contract.
use Laraveles\Rating\Traits\CanRate; use Laraveles\Rating\Contracts\Qualifier; class User extends Model implements Qualifier { use CanRate; ... }
The other models that can be rated should use CanBeRated trait and Rateable contract.
use Laraveles\Rating\Traits\CanBeRated; use Laraveles\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 Laraveles\Rating\Traits\Rate; use Laraveles\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->qualifiers()->get(); // Pages that have rated this page $page->qualifiers(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
Events
ModelRated
You can define your own listeners in your app's EventServiceProvider. E.g.:
<?php use Laraveles\Rating\Events\ModelRated; use Laraveles\Rating\Events\ModelUnrated; /** * The event listener mappings for the application. * * @var array */ protected $listen = [ ModelRated::class => [ \App\Listeners\MyListenerRating::class, ], ModelUnrated::class => [ \App\Listeners\MyListenerUnrating::class, ], ];
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email soj3da@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
sojeda/rating 适用场景与选型建议
sojeda/rating 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「laravel」 「system」 「Rating」 「eloquent」 「stars」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sojeda/rating 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sojeda/rating 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sojeda/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
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-28