User upvote/downvote behaviour for Laravel.
3.3.0
2024-03-18 14:48 UTC
Requires
- php: ^8.0
- illuminate/database: ^8.13 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0
- laravel-interaction/support: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- mockery/mockery: ~1.3.3 || ^1.4.2
- orchestra/testbench: ^6.0 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^9.3.3 || ^10.0
This package is auto-updated.
Last update: 2024-11-13 13:11:51 UTC
README
User upvote/downvote behaviour for Laravel.
Introduction
It let people express how they feel about the model(documentation/answer/question), is the model helpful/useful or not.
Installation
Requirements
Instructions
Require Laravel Vote using Composer.
composer require laravel-interaction/vote
Publish configuration and migrations
php artisan vendor:publish --tag=vote-config php artisan vendor:publish --tag=vote-migrations
Run database migrations.
php artisan migrate
Usage
Setup Voter
use Illuminate\Database\Eloquent\Model; use LaravelInteraction\Vote\Concerns\Voter; class User extends Model { use Voter; }
Setup Voteable
use Illuminate\Database\Eloquent\Model; use LaravelInteraction\Vote\Concerns\Voteable; class Channel extends Model { use Voteable; }
Voter
use LaravelInteraction\Vote\Tests\Models\Channel; /** @var \LaravelInteraction\Vote\Tests\Models\User $user */ /** @var \LaravelInteraction\Vote\Tests\Models\Channel $channel */ // Vote to Voteable $user->vote($channel); $user->upvote($channel); $user->downvote($channel); $user->cancelVote($channel); // Compare Voteable $user->hasVoted($channel); $user->hasNotVoted($channel); $user->hasUpvoted($channel); $user->hasNotUpvoted($channel); $user->hasDownvoted($channel); $user->hasNotDownvoted($channel); // Get voted info $user->voterVotes()->count(); // with type $user->voterVotes()->withType(Channel::class)->count(); $user->votedChannels()->count(); $user->upvotedChannels()->count(); $user->downvotedChannels()->count(); // get voted channels Channel::query()->whereVotedBy($user)->get(); Channel::query()->whereUpvotedBy($user)->get(); Channel::query()->whereDownvotedBy($user)->get(); // get voted channels doesnt voted Channel::query()->whereNotVotedBy($user)->get(); Channel::query()->whereNotUpvotedBy($user)->get(); Channel::query()->whereNotDownvotedBy($user)->get();
Voteable
use LaravelInteraction\Vote\Tests\Models\User; use LaravelInteraction\Vote\Tests\Models\Channel; /** @var \LaravelInteraction\Vote\Tests\Models\User $user */ /** @var \LaravelInteraction\Vote\Tests\Models\Channel $channel */ // Compare Voter $channel->isVotedBy($user); $channel->isNotVotedBy($user); $channel->isUpvotedBy($user); $channel->isNotUpvotedBy($user); $channel->isDownvotedBy($user); $channel->isNotDownvotedBy($user); // Get voters info $channel->voters->each(function (User $user){ echo $user->getKey(); }); $channel->upvoters->each(function (User $user){ echo $user->getKey(); }); $channel->downvoters->each(function (User $user){ echo $user->getKey(); }); $channels = Channel::query()->withCount('voters')->get(); $channels->each(function (Channel $channel){ echo $channel->voters()->count(); // 1100 echo $channel->voters_count; // "1100" echo $channel->votersCount(); // 1100 echo $channel->votersCountForHumans(); // "1.1K" echo $channel->upvoters()->count(); // 1100 echo $channel->upvoters_count; // "1100" echo $channel->upvotersCount(); // 1100 echo $channel->upvotersCountForHumans(); // "1.1K" echo $channel->downvoters()->count(); // 1100 echo $channel->downvoters_count; // "1100" echo $channel->downvotersCount(); // 1100 echo $channel->downvotersCountForHumans(); // "1.1K" });
Events
License
Laravel Vote is an open-sourced software licensed under the MIT license.