yoeunes/voteable

此包已被废弃且不再维护。未建议替代包。

此包可以帮助您将基于用户的投票系统添加到模型中。

v1.1 2021-02-28 09:42 UTC

README

👍 👎 此包可以帮助您将基于用户的投票系统添加到模型中。

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

您可以使用composer安装此包

$ composer require yoeunes/voteable

然后将服务提供者添加到config/app.php。在Laravel 5.5及更高版本中,如果启用了包自动发现,则可以跳过此步骤。

'providers' => [
    ...
    Yoeunes\Voteable\VoteableServiceProvider::class
    ...
];

发布迁移文件

$ php artisan vendor:publish --provider='Yoeunes\Voteable\VoteableServiceProvider' --tag="migrations"

作为可选步骤,如果您想修改默认配置,可以发布配置文件

$ php artisan vendor:publish --provider='Yoeunes\Voteable\VoteableServiceProvider' --tag="config"

并创建表

$ php artisan migrate

最后,将功能特性添加到User模型中

<?php

namespace App;

use Yoeunes\Voteable\Traits\Voteable;
use Illuminate\Database\Eloquent\Model;

class Lesson extends Model
{
    use Voteable;
}

使用方法

以下列出了所有可用的API。

Yoeunes\Voteable\Traits\Voteable

创建投票

$user   = User::first();
$lesson = Lesson::first();

$rating = $lesson->getVoteBuilder()
                 ->user($user) // you may also use $user->id
                 ->uniqueVoteForUsers(true) // update if already rated
                 ->amount(3);

更新投票

$lesson = Lesson::first();

$lesson->updateVote($vote_id, $amount); // vote_id and the new amount value
$lesson->updateVotesForUser($user_id, $amount); // update all votes for a single user related to the lesson

取消投票

$lesson = Lesson::first();
$lesson->cancelVote($vote_id); // delete a vote with the giving id
$lesson->cancelVotesForUser($user_id); // delete all votes for a single user related to the lesson
$lesson->resetVotes(); // delete all rating related to the lesson

检查模型是否已有投票

$lesson->isVoted();
$lesson->isUpVoted();
$lesson->isDownVoted();
$lesson->isUpVotedBy($user_id);// check if its already up voted by the given user
$lesson->isDownVotedBy($user_id);// check if its already up voted by the given user

获取投票计数

$lesson->upVotesCount();
$lesson->downVotesCount();
$lesson->votesCount(); // get the votes count (up votes + down votes)

获取投票模型的用户列表(投票者)

$lesson->voters()->get();
$lesson->voters()->where('name', 'like', '%yoeunes%')->get();
$lesson->voters()->orderBy('name')->get();

按日期获取投票计数

$lesson->countVotesByDate('2018-02-03 13:23:03', '2018-02-06 15:26:06');
$lesson->countVotesByDate('2018-02-03 13:23:03');
$lesson->countVotesByDate(null, '2018-02-06 15:26:06');
$lesson->countVotesByDate(Carbon::now()->parse('01-04-2017'), Carbon::now()->parse('01-06-2017'));
$lesson->countVotesByDate(Carbon::now()->subDays(2));

其他API方法

Lesson::select('lessons.*')->orderByAverageUpVotes('asc')->get()
Lesson::select('lessons.*')->orderByAverageDownVotes('desc')->get()

查询关系

$ratings = $user->votes
$ratings = $user->votes()->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->countVotesByDate('past3days');

许可证

MIT