yoeunes / rateable
此包已被废弃,不再维护。未建议替代包。
此包可以帮助您将基于用户的评分系统添加到模型中。
v1.0.2
2018-02-04 16:38 UTC
Requires
- php: >=7.0.0
- illuminate/database: ^5.5
- illuminate/support: ^5.5
Requires (Dev)
- laracasts/testdummy: ^2.3
- orchestra/testbench: ^3.5
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2022-07-13 06:08:51 UTC
README
❤️ 此包可以帮助您将基于用户的评分系统添加到模型中。
您可以使用composer安装此包
$ composer require yoeunes/rateable
然后,将服务提供者添加到config/app.php
文件中。在Laravel 5.5及更高版本中,如果启用了包自动发现,则可以跳过此步骤。
'providers' => [ ... Yoeunes\Rateable\RateableServiceProvider::class ... ];
发布迁移文件
$ php artisan vendor:publish --provider='Yoeunes\Rateable\RateableServiceProvider' --tag="migrations"
如果需要修改默认配置,可以发布配置文件作为可选步骤
$ php artisan vendor:publish --provider='Yoeunes\Rateable\RateableServiceProvider' --tag="config"
并创建表
$ php artisan migrate
最后,将功能特质添加到User模型中
<?php namespace App; use Yoeunes\Rateable\Traits\Rateable; use Illuminate\Database\Eloquent\Model; class Lesson extends Model { use Rateable; }
用法
以下列出了所有可用的API。
Yoeunes\Rateable\Traits\Rateable
创建评分
$user = User::first(); $lesson = Lesson::first(); $rating = $lesson->getRatingBuilder() ->user($user) // you may also use $user->id ->uniqueRatingForUsers(true) // update if already rated ->rate(3);
更新评分
$lesson = Lesson::first(); $lesson->updateRating($rating_id, $value); // rating_id and the new rating value $lesson->updateRatingForUser($user_id, $value); // update all rating for a single user related to the lesson
删除评分
$lesson = Lesson::first(); $lesson->deleteRating($rating_id); // delete a rating with the giving id $lesson->deleteRatingsForUser($user_id); // delete all rating for a single user related to the lesson $lesson->resetRating(); // delete all rating related to the lesson
检查模型是否已被评分
$lesson->isRated(); $lesson->isRatedBy($user_id);// check if its already rated by the given user
获取平均评分
$lesson->averageRating(); // get the average rating $lesson->averageRatingForUser($user_id); // get the average rating for a single user
获取评分模型的用户列表(评分者)
$lesson->raters()->get(); $lesson->raters()->where('name', 'like', '%yoeunes%')->get(); $lesson->raters()->orderBy('name')->get();
获取日期之间的评分数量
$lesson->countRatingsByDate('2018-02-03 13:23:03', '2018-02-06 15:26:06'); $lesson->countRatingsByDate('2018-02-03 13:23:03'); $lesson->countRatingsByDate(null, '2018-02-06 15:26:06'); $lesson->countRatingsByDate(Carbon::now()->parse('01-04-2017'), Carbon::now()->parse('01-06-2017')); $lesson->countRatingsByDate(Carbon::now()->subDays(2));
其他API方法
$lesson->countRating() $lesson->countRatingForUser($user_id) $lesson->totalRating() $lesson->totalRatingForUser($user_id) $lesson->ratingPercentage() $lesson->positiveRatingCount() $lesson->positiveRatingTotal() $lesson->negativeRatingCount() $lesson->negativeRatingTotal() Lesson::select('lessons.*')->orderByAverageRating('asc')->get() Lesson::select('lessons.*')->orderByAverageRating('desc')->get()
查询关系
$ratings = $user->ratings $ratings = $user->ratings()->where('id', '>', 10)->get()
日期转换器
因为我们都喜欢重复更少的事情,这个包允许您定义日期转换器。比如说,我们经常使用以下代码:$lesson->countRatingsByDate(Carbon::now()->subDays(3))。这可能会变得有点让人烦恼和不耐看。让我们来解决这个问题吧!
如果您已发布配置文件,您将看到如下内容
'date-transformers' => [ // 'past24hours' => Carbon::now()->subDays(1), // 'past7days' => Carbon::now()->subWeeks(1), // 'past14days' => Carbon::now()->subWeeks(2), ],
默认情况下,它们都被注释掉了。要使它们可用,只需取消注释即可。提供的示例仅供参考。您可以删除它们或添加自己的。
'date-transformers' => [ 'past3days' => Carbon::now()->subDays(3), ],
现在我们可以这样检索评分数量
$lesson->countRatingsByDate('past3days');
许可证
MIT