melsaka/voteable

一个 Laravel 扩展包,允许您轻松地在 Laravel 应用程序中实现投票系统。

dev-main 2023-09-12 00:07 UTC

This package is auto-updated.

Last update: 2024-09-12 02:17:45 UTC


README

Laravel Voteable 扩展包提供了一个方便的方式来在您的 Laravel 应用程序中实现投票系统。使用这个包,您可以轻松地允许用户(投票者)对应用程序中的各种模型(可投票项)进行投票,例如帖子、评论或其他任何可投票内容。

安装

通过 Composer 将包添加到您的 Laravel 应用程序中。

composer require melsaka/voteable

在 config/app.php 中注册包的 service provider。

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

运行迁移以将所需的表添加到您的数据库中。

php artisan migrate

Voter 特性添加到投票者模型中,例如 User 模型。

use Melsaka\Voteable\Voter;

class User extends Model
{
    use Voter;
    
    // ...   
}

Voteable 特性添加到您的可投票模型中,例如 Post 模型。

use Melsaka\Voteable\Voteable;

class Post extends Model
{
    use Voteable;
    
    // ...   
}

配置

为了配置包,发布其配置文件。

php artisan vendor:publish --tag=voteable

然后,您可以修改配置文件以更改投票表名(如果需要),默认为:votes

使用方法

Laravel Voteable 包提供了一系列处理投票的方法。以下是一些关键功能:

为了演示,我们将使用 UserPost 模型作为示例,其中 $user 是投票者,$post 是可投票项。

$user = User::first(); // voter
$post = Post::first(); // voteable

对帖子进行投票

您可以使用以下方法对帖子进行点赞或踩。

// Vote up to this post by a voter (user)
Vote::up($post, $user);

// Vote down to this post by a voter (user)
Vote::down($post, $user);

// Remove a voter's (user's) vote on this post
Vote::remove($post, $user);

对帖子进行投票

您可以使用以下方法检查投票者(用户)是否对帖子进行了投票。

// Check if a voter (user) has voted on this post
Vote::has($post, $user);

其他投票方法

您还可以使用可投票和投票者实例提供的方法。

// Vote up a post using the post instance
$post->upVote($user);

// Vote down a post using the post instance
$post->downVote($user);

// Remove a vote on a post using the post instance
$post->removeVote($user);

// Check if a user has voted on a post using the post instance
$post->hasVote($user);

// You can perform similar actions using the voter instance
$user->upVote($post);
$user->downVote($post);
$user->removeVote($post);
$user->hasVote($post);

检索投票者和已投票项

您可以检索对特定可投票项进行投票的所有投票者(用户)和特定投票者(用户)的所有已投票项。

// Get all voters (users) who voted on a post
$post->voters(User::class)->get();

// Get all voted posts by a voter (user)
$user->voteables(Post::class)->get();

预加载投票数据

您还可以预加载可投票模型的投票数和投票总和。

// Eager load the total sum of the post votes
Post::withVotesSum()->get();
$post->loadVotesSum();

// Eager load the number of the post votes
Post::withVotesCount()->get();
$post->loadVotesCount();

// Eager load only the number of the post up votes
Post::withUpVotesCount()->get();
$post->loadUpVotesCount();

// Eager load only the number of the post down votes
Post::withDownVotesCount()->get();
$post->loadDownVotesCount();

按投票排序

您可以按投票数或投票总和对可投票模型(如帖子)进行排序。

// Order posts by the number of votes
Post::orderByVotesCount()->get();

// Order posts by the total sum of votes
Post::orderByVotesSum()->get();

检查投票者是否对多个项进行了投票

您可以获取帖子并检查特定投票者(用户)是否对它们进行了投票。

Post::withVoted($user);

访问投票关系

您可以通过 votes() 方法访问投票关系。

// Access the votes relation for a post
$post->votes();

// Access the votes relation for a voter (user)
$user->votes();

数据库模式

为了存储投票数据,该包使用以下数据库模式:

Schema::create('votes', function (Blueprint $table) {
    $table->id();
    $table->morphs('voteable');
    $table->morphs('voter');
    $table->tinyInteger('vote');
    $table->timestamps();
});

许可证

此包根据 MIT 许可证(MIT)发布。