PHP code example of jcc / laravel-vote

1. Go to this page and download the library: Download jcc/laravel-vote library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jcc / laravel-vote example snippets


Jcc\LaravelVote\VoteServiceProvider::class

use Jcc\LaravelVote\Traits\Voter;

class User extends Model
{
    use Voter;
}

use Jcc\LaravelVote\Traits\Votable;

class Comment extends Model
{
    use Votable;
}

$comment = Comment::find(1);

$user->upVote($comment);

$comment = Comment::find(1);

$user->downVote($comment);

$comment = Comment::find(1);

$user->cancelVote($comment);

$user->getVotedItems(Comment::class)->get();

$comment = Comment::find(1);

$user->hasVoted($comment);

$comment = Comment::find(1);

$user->hasUpVoted($comment);

$comment = Comment::find(1);

$user->hasDownVoted($comment);

$comment->voters()->get();

$comment->voters()->count();

$comment->upVoters()->get();

$comment->upVoters()->count();

$comment->downVoters()->get();

$comment->downVoters()->count();

$user = User::find(1);

$comment->isVotedBy($user);

$user = User::find(1);

$comment->isUpVotedBy($user);

$user = User::find(1);

$comment->isDownVotedBy($user);

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