overtrue / laravel-vote
Laravel 应用程序的用户投票功能。
3.2.0
2024-03-13 05:06 UTC
Requires
- laravel/framework: ^9.0|^10.0|^11.0
Requires (Dev)
- brainmaestro/composer-git-hooks: dev-master
- friendsofphp/php-cs-fixer: ^3.6
- laravel/pint: ^1.5
- mockery/mockery: ^1.4
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
⬆️ ⬇️ Laravel 应用程序的用户投票系统。
安装
composer require overtrue/laravel-vote -vvv
配置与迁移
php artisan vendor:publish
然后创建表
php artisan migrate
用法
特性
Overtrue\LaravelVote\Traits\Voter
use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Overtrue\LaravelVote\Traits\Voter; class User extends Authenticatable { use Voter; <...> }
Overtrue\LaravelVote\Traits\Voteable
use Illuminate\Database\Eloquent\Model; use Overtrue\LaravelVote\Traits\Votable; class Idea extends Model { use Votable; <...> }
API
$user = User::find(1); $idea = Idea::find(2); $user->vote($idea, 1); // upvote $user->vote($idea, -1); // downvote $user->upvote($idea); $user->downvote($idea); // with custom number of votes $user->upvote($idea, 3); $user->downvote($idea, 3); // cancel vote $user->cancelVote($idea); // get my voted items $user->getVotedItems(Idea::class) // Illuminate\Database\Eloquent\Builder // state $user->hasVoted($idea); $idea->hasBeenVotedBy($user);
获取模型投票者
foreach($idea->voters as $user) { // echo $user->name; }
获取用户投票项。
用户可以轻松获取可投票模型以执行您想要的操作。
*注意:此方法将返回一个 Illuminate\Database\Eloquent\Builder
*
$votedItemsQuery = $user->getVotedItems(); // filter votable_type $votedIdeasQuery = $user->getVotedItems(Idea::class); // fetch results $votedIdeas = $user->getVoteItems(Idea::class)->get(); $votedIdeas = $user->getVoteItems(Idea::class)->paginate(); $votedIdeas = $user->getVoteItems(Idea::class)->where('title', 'Laravel-Vote')->get();
聚合
计数关系
// all $user->votes()->count(); // filter votable_type $user->votes()->ofType(Idea::class)->count(); // voters count $idea->voters()->count();
使用 *_count
属性列出
// for Voter models: $users = User::withCount('votes')->get(); // or $users = User::withCount('upvotes')->get(); // or $users = User::withCount('downvotes')->get(); // or $users = User::withCount(['votes', 'upvotes', 'downvotes'])->get(); foreach($users as $user) { echo $user->votes_count; echo $user->upvotes_count; echo $user->downvotes_count; } // for Votable models: $ideas = Idea::withCount('voters')->get(); // or $ideas = Idea::withCount('upvoters')->get(); $ideas = Idea::withCount('downvoters')->get(); // or $ideas = Idea::withCount(['voters', 'upvoters', 'downvoters'])->get(); foreach($ideas as $idea) { echo $idea->voters_count; echo $idea->upvoters_count; echo $idea->downvoters_count; }
可投票的投票总和
$user1->upvote($idea); // 1 (up) $user2->upvote($idea); // 2 (up) $user3->upvote($idea); // 3 (up) $user4->downvote($idea); // -1 (down) // sum(votes) $idea->totalVotes(); // 2(3 - 1) // sum(votes) where votes > 0 $idea->totalUpvotes(); // 3 // abs(sum(votes)) where votes < 0 $idea->totalDownvotes(); // 1 // appends aggregations attributes $idea->appendsVotesAttributes(); $idea->toArray(); // result [ "id" => 1 "title" => "Add socialite login support." "created_at" => "2021-05-20T03:26:16.000000Z" "updated_at" => "2021-05-20T03:26:16.000000Z" // these aggregations attributes will be appends. "total_votes" => 2 "total_upvotes" => 3 "total_downvotes" => 1 ],
将投票者的投票状态附加到可投票集合中
您可以使用 Voter::attachVoteStatus(Collection $votables)
来附加投票者的投票状态,它将为 $votables
中的每个模型设置 has_voted
、has_upvoted
和 has_downvoted
属性
对于模型
$idea = Idea::find(1); $user->attachVoteStatus($idea); // result [ "id" => 1 "title" => "Add socialite login support." "created_at" => "2021-05-20T03:26:16.000000Z" "updated_at" => "2021-05-20T03:26:16.000000Z" "has_voted" => true "has_upvoted" => true "has_downvoted" => false ],
对于 Collection | Paginator | LengthAwarePaginator | array
$ideas = Idea::oldest('id')->get(); $user->attachVoteStatus($ideas); $ideas = $ideas->toArray(); // result [ [ "id" => 1 "title" => "Add socialite login support." "created_at" => "2021-05-20T03:26:16.000000Z" "updated_at" => "2021-05-20T03:26:16.000000Z" "has_voted" => true "has_upvoted" => true "has_downvoted" => false ], [ "id" => 2 "title" => "Add php8 support." "created_at" => "2021-05-20T03:26:16.000000Z" "updated_at" => "2021-05-20T03:26:16.000000Z" "has_voted" => true "has_upvoted" => false "has_downvoted" => true ], [ "id" => 3 "title" => "Add qrcode support." "created_at" => "2021-05-20T03:26:16.000000Z" "updated_at" => "2021-05-20T03:26:16.000000Z" "has_voted" => false "has_upvoted" => false "has_downvoted" => false ], ]
对于分页
$ideas = Idea::paginate(20); $user->attachVoteStatus($ideas->getCollection());
N+1 问题
为了避免 N+1 问题,您可以使用预加载来将此操作减少到仅 2 个查询。在查询时,您可以使用 with
方法指定应该预加载哪些关系
// Voter use Tests\Idea;$users = User::with('votes')->get(); foreach($users as $user) { $user->hasVoted($idea); } // Votable $ideas = Idea::with('voters')->get(); foreach($ideas as $idea) { $idea->hasBeenVotedBy($user); } // Votable votes $ideas = Idea::withTotalVotes() // total_votes ->withTotalUpvotes() // total_upvotes ->withTotalDownvotes() // total_downvotes ->get(); // same as // withVotesAttributes() = withTotalVotes() + withTotalUpvotes() + withTotalDownvotes() $ideas = Idea::withVotesAttributes()->get(); // result [ [ "id" => 1 "title" => "Add socialite login support." "created_at" => "2021-05-19T07:01:10.000000Z" "updated_at" => "2021-05-19T07:01:10.000000Z" "total_votes" => 2 "total_upvotes" => 3 "total_downvotes" => 1 ], [ "id" => 2 "title" => "Add PHP8 support." "created_at" => "2021-05-20T07:01:10.000000Z" "updated_at" => "2021-05-20T07:01:10.000000Z" "total_votes" => 1 "total_upvotes" => 2 "total_downvotes" => 1 ] ]
事件
相关包
- 关注:overtrue/laravel-follow
- 喜欢:overtrue/laravel-like
- 投票:overtrue/laravel-vote
- 订阅:overtrue/laravel-subscribe
- 书签:overtrue/laravel-bookmark (进行中)
❤️ 赞助我
如果您喜欢我的项目并想支持它,点击这里 ❤️
由 JetBrains 支持的项目
非常感谢 JetBrains 善意提供许可证,使我能够参与这个和其他开源项目的工作。
贡献
您可以通过以下三种方式之一进行贡献
代码贡献过程并不十分正式。您只需确保遵循 PSR-0、PSR-1 和 PSR-2 编码指南。任何新的代码贡献都必须附带适用的情况下的单元测试。
PHP 扩展包开发
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
许可
MIT