⬆️ ⬇️ User vote system for Laravel Application.

Overview

Laravel Vote

⬆️ ⬇️ User vote system for Laravel Application.

CI

Installing

$ composer require overtrue/laravel-vote -vvv

Configuration

This step is optional

$ php artisan vendor:publish --provider="Overtrue\\LaravelVote\\VoteServiceProvider" --tag=config

Migrations

This step is required, you can publish the migration files:

$ php artisan vendor:publish --provider="Overtrue\\LaravelVote\\VoteServiceProvider" --tag=migrations

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

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

Comments
  • Why total votes are returned as an absolute value?

    Why total votes are returned as an absolute value?

    For example if there are more downvotes shouldn't it return a negative value? https://github.com/overtrue/laravel-vote/blob/8573e50e88b9cd43ea1037948bf216bc1edaf886/src/Traits/Votable.php#L77

    opened by gordanielyan 4
  • During class fetch: Uncaught ParseError: syntax error, unexpected '|', expecting variable (T_VARIABLE) in /vendor/overtrue/laravel-vote/src/Traits/Voter.php

    During class fetch: Uncaught ParseError: syntax error, unexpected '|', expecting variable (T_VARIABLE) in /vendor/overtrue/laravel-vote/src/Traits/Voter.php

    Theres is an error in attachVoteStatus() Method in Voter Trait. public function attachVoteStatus(Model | Collection | Paginator | LengthAwarePaginator | array $votables): Collection | Model

    opened by gitamgadtaula 3
  • downvote count not working as expected

    downvote count not working as expected

    I have created a function in my livewire components

    public function downvote(Post $post)
    {
      auth()->user()->downvote($post)
    }
    
    I noticed when I downvote a post the count increase for the upvote instead
    opened by Chizzycute 1
  • Fix total votes scope to remove redundant DB query

    Fix total votes scope to remove redundant DB query

    If you try to get something like Idea::withTotalVotes()->get() the attribute total_votes can be null if there are no votes, which will result in additional query because of this fallback https://github.com/overtrue/laravel-vote/blob/f06ad18d7b509cc2f3d507ffb6afd7a0d0e6775b/src/Traits/Votable.php#L77

    i suggest to use COALESCE to make it zero when no records are found

    opened by gordanielyan 0
  • DB Unique constraint for good measure?

    DB Unique constraint for good measure?

    There is no unique DB constraint on user_id, votable_type, votable_id. One could easily be added, maybe it was not included if this feature is not available in all databases?

    $table->unique(['user_id', 'votable_type', 'votable_id']);
    
    opened by LiamKarlMitchell 0
  • Revoting does not retain original created_at

    Revoting does not retain original created_at

    Because the previous record is deleted by upvote or downvote calling cancelVote then a new one is added. Why not just update the existing vote?

    Maybe it is not a big deal, and could be because of the dispatching of Voted and VoteCancelled events, but means updated_at is a redundant field.

    opened by LiamKarlMitchell 0
Releases(3.0.4)
  • 3.0.4(Aug 12, 2022)

    What's Changed

    • Fix total votes scope to remove redundant DB query by @gordanielyan in https://github.com/overtrue/laravel-vote/pull/6

    New Contributors

    • @gordanielyan made their first contribution in https://github.com/overtrue/laravel-vote/pull/6

    Full Changelog: https://github.com/overtrue/laravel-vote/compare/3.0.3...3.0.4

    Source code(tar.gz)
    Source code(zip)
  • 3.0.3(Aug 11, 2022)

  • 3.0.2(Aug 11, 2022)

  • 3.0.1(Jul 4, 2022)

    What's Changed

    • add withPivot to Votable voters by @xyxu in https://github.com/overtrue/laravel-vote/pull/4

    New Contributors

    • @xyxu made their first contribution in https://github.com/overtrue/laravel-vote/pull/4

    Full Changelog: https://github.com/overtrue/laravel-vote/compare/3.0.0...3.0.1

    Source code(tar.gz)
    Source code(zip)
  • 3.0.0(Feb 10, 2022)

  • 2.0.0(May 21, 2021)

    • rename upVote() to upvote(), downVote() to downvote()
    • rename has_up_voted to has_upvoted, has_down_voted to has_downvoted
    • rename total_up_votes to total_upvotes, total_down_votes to total_downvotes
    Source code(tar.gz)
    Source code(zip)
Owner
安正超
Keep calm and coding.
安正超
Hydra is a zero-config API boilerplate with Laravel Sanctum that comes with excellent user and role management API out of the box

Hydra - Zero Config API Boilerplate with Laravel Sanctum Hydra is a zero-config API boilerplate with Laravel Sanctum and comes with excellent user and

Hasin Hayder 858 Dec 24, 2022
This is a laravel Auth Starter Kit, with full user/admin authentication with both session and token auth

About Auth Starter It's a Laravel 8 authentication markdown that will help you to understand and grasp all the underlying functionality for Session an

Sami Alateya 10 Aug 3, 2022
Use this skeleton application to quickly setup and start working on a new Slim Framework 4 application

Slim Framework 4 Skeleton Application Use this skeleton application to quickly setup and start working on a new Slim Framework 4 application. This app

Slim Framework 1.5k Dec 25, 2022
Users Management for extension user.

Template for Yii Packages Installation composer require <vendor/your-packages> Unit testing The package is tested with PHPUnit. To run tests: ./vendor

null 1 Nov 24, 2021
Menyimpan source code UTS Mata kuliah Rekayasa Web tentang User Authentication menggunakan Session dan Cookie. Deployed on Heroku.

About Academica Academica adalah website dengan tema edukasi yang mengimplementasikan user autensikasi menggunakan session dan cookie. Halaman dashboa

Galang Aidil Akbar 2 Nov 25, 2021
A new blog system based on laravel+vue

Lumen PHP Framework Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with expressive, elegant syntax. We believe d

white 14 Nov 4, 2022
A Event Management system - EMS build with Laravel and Vuejs

Event Management system - EMS A Event Management system - EMS build with Laravel and Vuejs Both FE & BE project folders has own README.md files for in

hassan 5 Jul 21, 2022
Laravel and AngularJS Starter Application Boilerplate featuring Laravel 5.3 and AngularJS 1.5.8

?? Zemke/starter-laravel-angular has been upgraded to AngularJS 1.5.8. ?? Zemke/starter-laravel-angular has been upgraded to Laravel 5.3. You can pull

Florian Zemke 372 Nov 21, 2022
Laravel Quick-Start - a boilerplate for Laravel Application with typical packages preinstalled and configured

Laravel Quickstart is a boilerplate for Laravel Application with typical packages preinstalled and configured to extend a full-fledged application. We tried to make it as minimal as possible.

Vijay Goswami 18 Sep 8, 2022
An account management Panel based on Laravel7 framework. Include multiple payment, account management, system caching, admin notification, products models, and more.

ProxyPanel 简体中文 Support but not limited to: Shadowsocks,ShadowsocksR,ShadowsocksRR,V2Ray,Trojan,VNET Demo Demo will always on dev/latest code, rather

null 17 Sep 3, 2022
Syntax-aware proofreading for your Laravel application.

Laravel Prose Linter Syntax-aware proofreading for your Laravel application. The Laravel Prose Linter helps you to polish the texts of your Laravel ap

Beyond Code 97 Dec 18, 2022
Creating a simple weather web application with Laravel rest API.

Weather Channel Creating a website for weather status, with Laravel restAPI. See the Website Weather.Channel-1.mov Features REST API Invoake Controlle

AmirH.Najafizadeh 3 Jul 31, 2022
A Laravel admin panel which is creating CRUD for your application automatically.

Adds a zero configuration Admin Panel to your Laravel Application Installation You can install the package via composer: composer require max-hutschen

42coders 10 Aug 24, 2022
Registry manager for Laravel 5. An alternative for managing application configurations and settings.

Registry manager for Laravel 5. An alternative for managing application configurations and settings. Now with the magic of caching, so no more database calls to simply get site setting.

Daniel Stainback 22 Sep 28, 2020
Quick new application creation with Laravel and Valet

Super-powered laravel new for Laravel and Valet Lambo is a command-line tool that replaces the Laravel installer and wraps up the most common tasks yo

Tighten 593 Dec 30, 2022
Laravel CRUD Generator, Make a Web Application Just In Minutes, Even With Less Code and fewer Steps !

?? CRUDBOOSTER - Laravel CRUD Generator Laravel CRUD Generator, Make a Web Application Just In Minutes, Even With Less Code and fewer Steps ! About CR

Crocodic Studio 1.7k Jan 8, 2023
👔 Enterprise Web application starter kit or template using Laravel

Laravel Enterprise Starter Kit (LESK) Description LESK, is a template project based on the Laravel LTS, combining a set of features that can kick star

Sebastien Routier 1 Dec 31, 2020
Laravel Framework 5 Bootstrap 3 Starter Site is a basic application with news, photo and video galeries.

Laravel Framework 5.1 Bootstrap 3 Starter Site Starter Site based on on Laravel 5.1 and Boostrap 3 Features Requirements How to install Application St

null 900 Dec 22, 2022
Demo and practice application with Laravel 8 and InertiaJS. (From laracasts course)

InertiaJS playground ⚽️ Started with the Laracasts: Build Modern Laravel Apps Using Inertia.js course and decided to share all my code here, I'll be a

Kasper Ligthart 1 Dec 2, 2021