2017 © Pedro Peláez
 

library laravel-vote

The package helps you to add user based vote system to your model

image

jcc/laravel-vote

The package helps you to add user based vote system to your model

  • Thursday, January 11, 2018
  • by jcc
  • Repository
  • 2 Watchers
  • 81 Stars
  • 10,602 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 8 Forks
  • 1 Open issues
  • 8 Versions
  • 9 % Grown

The README.md

Laravel Vote System

:tada: This package helps you to add user based vote system to your model., (*1)

Installation

You can install the package using Composer:, (*2)

$ composer require "jcc/laravel-vote:~2.0"

Then add the service provider to config/app.php:, (*3)

Jcc\LaravelVote\VoteServiceProvider::class

Publish the migrations file:, (*4)

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

Finally, use VoteTrait in User model:, (*5)

use Jcc\LaravelVote\Traits\Voter;

class User extends Model
{
    use Voter;
}

Or use CanBeVoted in Comment model:, (*6)

use Jcc\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:, (*7)

// 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
Jcc\LaravelVote\Events\Voted Triggered when the relationship is created or updated.
Jcc\LaravelVote\Events\CancelVoted Triggered when the relationship is deleted.

Reference

License

MIT, (*8)

The Versions

11/01 2018

dev-master

9999999-dev

The package helps you to add user based vote system to your model

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar jcc

11/01 2018

v1.1.4

1.1.4.0

The package helps you to add user based vote system to your model

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar jcc

03/05 2017

v1.1.3

1.1.3.0

The package helps you to add user based vote system to your model

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar jcc

26/04 2017

v1.1.2

1.1.2.0

The package helps you to add user based vote system to your model

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar jcc

25/04 2017

v1.1.1

1.1.1.0

The package helps you to add user based vote system to your model

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar jcc

18/04 2017

v1.1.0

1.1.0.0

The package helps you to add user based vote system to your model

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar jcc

17/04 2017

v1.0.1

1.0.1.0

The package helps you to add user based vote system to your model

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar jcc

14/04 2017

v1.0.0

1.0.0.0

The package helps you to add user based vote system to your model

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Avatar jcc