All Projects → overtrue → laravel-vote

overtrue / laravel-vote

Licence: other
⬆️ ⬇️ User vote system for Laravel Application.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-vote

Vue Poll
A Vue.js component for voting
Stars: ✭ 115 (+116.98%)
Mutual labels:  vote
Raty
🌟 Raty - A Star Rating Plugin
Stars: ✭ 2,292 (+4224.53%)
Mutual labels:  vote
freedomvote
A tool to represent the views of politicians and parties as a help to the voters.
Stars: ✭ 15 (-71.7%)
Mutual labels:  vote
Django Vote
Simple vote for django
Stars: ✭ 141 (+166.04%)
Mutual labels:  vote
Blockvotes
An e-voting system based on blockchain using ring signature
Stars: ✭ 182 (+243.4%)
Mutual labels:  vote
rater-js
Star rating widget for the browser. Unlimited number of stars. No dependencies. No Jquery required.
Stars: ✭ 66 (+24.53%)
Mutual labels:  vote
Steem Fossbot Voter
A curation bot built for the Steemit social media platform, decides which posts to vote for and casts vote on behalf of a registered user
Stars: ✭ 92 (+73.58%)
Mutual labels:  vote
Votes
LarkVote投票系统是一套基于规则的企业内部投票系统,由投票管理和投票端两部分组成,投票管理端包括投票创建、投票编辑、规则编辑、投票发起、投票项管理、邀请码生成、结束投票、投票结果统计等功能;投票端包括投票规则展示、投票操作、提交结果等功能。目前该项目还处于初级阶段,欢迎各位大神创建pull request。
Stars: ✭ 60 (+13.21%)
Mutual labels:  vote
Larapoll
A Laravel package to manage your polls
Stars: ✭ 189 (+256.6%)
Mutual labels:  vote
PollDaddyHack
Exploit PollDaddy polls
Stars: ✭ 33 (-37.74%)
Mutual labels:  vote
Polls
🗳️ Polls app for Nextcloud
Stars: ✭ 144 (+171.7%)
Mutual labels:  vote
Gottovote
GotToVote is a toolkit of simple web and SMS services that help citizens get to the ballot box informed and ready to vote. Kenya version accessible at https://kenya.gottovote.cc
Stars: ✭ 150 (+183.02%)
Mutual labels:  vote
ballotnav
A repository for HackforLA's BallotNav project
Stars: ✭ 21 (-60.38%)
Mutual labels:  vote
Gh Polls
These polls work by pasting individual markdown SVG images into your issue, each wrapped with a link that tracks a vote. A single vote per IP is allowed for a given poll, which are stored in DynamoDB.
Stars: ✭ 1,726 (+3156.6%)
Mutual labels:  vote
vote-schulze
Vote calculation with Schulze method (Condorcet voting)
Stars: ✭ 23 (-56.6%)
Mutual labels:  vote
Dapp
Censorship resistant democracies.
Stars: ✭ 1,326 (+2401.89%)
Mutual labels:  vote
Laravel-rating
Laravel package that allows you to rate, like & dislike and vote(+1,-1) your models with a simple and clear ways
Stars: ✭ 204 (+284.91%)
Mutual labels:  vote
rating
⭐ A true Bayesian rating system with scope and cache enabled
Stars: ✭ 49 (-7.55%)
Mutual labels:  vote
yii2-vote
Provides voting for any model 👍 👎
Stars: ✭ 70 (+32.08%)
Mutual labels:  vote
WoWSimpleRegistration
Simple Registration page for TrinityCore/AzerothCore/AshamaneCore/CMangos
Stars: ✭ 121 (+128.3%)
Mutual labels:  vote

Laravel Vote

⬆️ ⬇️ User vote system for Laravel Application.

CI

Sponsor me

Installing

composer require overtrue/laravel-vote -vvv

Configuration & Migrations

php artisan vendor:publish 

then create tables:

php artisan migrate

Usage

Traits

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); 

Get model voters:

foreach($idea->voters as $user) {
    // echo $user->name;
}

Get user voted items.

User can easy to get Votable models to do what you want.

*note: this method will return a 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();

Aggregations

count relations

// all
$user->votes()->count(); 

// filter votable_type
$user->votes()->ofType(Idea::class)->count(); 

// voters count
$idea->voters()->count();

List with *_count attribute:

// 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;
}

Votable sum votes

$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
  ],

Attach voter vote status to votable collection

You can use Voter::attachVoteStatus(Collection $votables) to attach the voter vote status, it will set has_voted,has_upvoted and has_downvoted attributes to each model of $votables:

For model

$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
 ],

For 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
  ],
]

For pagination

$ideas = Idea::paginate(20);

$user->attachVoteStatus($ideas->getCollection());

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
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
  ]
]

Events

Event Description
Overtrue\LaravelVote\Events\Voted Triggered when the relationship is created.
Overtrue\LaravelVote\Events\VoteCancelled Triggered when the relationship is deleted.

Related packages

❤️ Sponsor me

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].