samuelsihotang1/laravel-vote
Composer 安装命令:
composer require samuelsihotang1/laravel-vote
包简介
The package helps you to add user based vote system to your model, fork from jcc/laravel-vote
README 文档
README
🎉 This package helps you to add user based vote system to your model.
Installation
You can install the package using Composer:
$ composer require "samuelsihotang1/laravel-vote:~2.0"
Then add the service provider to config/app.php:
samuelsihotang1\LaravelVote\VoteServiceProvider::class
Publish the migrations file:
$ php artisan vendor:publish --provider="samuelsihotang1\LaravelVote\VoteServiceProvider" --tag="migrations"
Finally, use VoteTrait in User model:
use samuelsihotang1\LaravelVote\Traits\Voter; class User extends Model { use Voter; }
Or use CanBeVoted in Comment model:
use samuelsihotang1\LaravelVote\Traits\Votable; class Comment extends Model { use Votable; }
Usage
For User model
Up vote a comment or comments
$comment = Comment::find(1); $user->upVote($comment);
Down vote a comment or comments
$comment = Comment::find(1); $user->downVote($comment);
Cancel vote a comment or comments
$comment = Comment::find(1); $user->cancelVote($comment);
Get user has voted comment items
$user->getVotedItems(Comment::class)->get();
Check if user has up or down vote
$comment = Comment::find(1); $user->hasVoted($comment);
Check if user has up vote
$comment = Comment::find(1); $user->hasUpVoted($comment);
Check if user has down vote
$comment = Comment::find(1); $user->hasDownVoted($comment);
For Comment model
Get comment voters
$comment->voters()->get();
Count comment voters
$comment->voters()->count();
Get comment up voters
$comment->upVoters()->get();
Count comment up voters
$comment->upVoters()->count();
Get comment down voters
$comment->downVoters()->get();
Count comment down voters
$comment->downVoters()->count();
Check if voted by
$user = User::find(1); $comment->isVotedBy($user);
Check if up voted by
$user = User::find(1); $comment->isUpVotedBy($user);
Check if down voted by
$user = User::find(1); $comment->isDownVotedBy($user);
N+1 issue
To avoid the N+1 issue, you can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the with method:
// Voter $users = User::with('votes')->get(); foreach($users as $user) { $user->hasVoted($comment); } // Votable $comments = Comment::with('voters')->get(); foreach($comments as $comment) { $comment->isVotedBy($user); }
Events
| Event | Description |
|---|---|
samuelsihotang1\LaravelVote\Events\Voted |
Triggered when the relationship is created or updated. |
samuelsihotang1\LaravelVote\Events\CancelVoted |
Triggered when the relationship is deleted. |
Reference
License
samuelsihotang1/laravel-vote 适用场景与选型建议
samuelsihotang1/laravel-vote 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 samuelsihotang1/laravel-vote 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 samuelsihotang1/laravel-vote 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-22