GithubHelp home page GithubHelp logo

voteable's Introduction

Laravel Voting System

๐Ÿ‘ ๐Ÿ‘Ž This package helps you to add user based voting system to your model.

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

You can install the package using composer

$ composer require yoeunes/voteable

Then add the service provider to config/app.php. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.

'providers' => [
    ...
    Yoeunes\Voteable\VoteableServiceProvider::class
    ...
];

Publish the migrations file:

$ php artisan vendor:publish --provider='Yoeunes\Voteable\VoteableServiceProvider' --tag="migrations"

As optional if you want to modify the default configuration, you can publish the configuration file:

$ php artisan vendor:publish --provider='Yoeunes\Voteable\VoteableServiceProvider' --tag="config"

And create tables:

$ php artisan migrate

Finally, add feature trait into User model:

<?php

namespace App;

use Yoeunes\Voteable\Traits\Voteable;
use Illuminate\Database\Eloquent\Model;

class Lesson extends Model
{
    use Voteable;
}

Usage

All available APIs are listed below.

Yoeunes\Voteable\Traits\Voteable

Create a vote

$user   = User::first();
$lesson = Lesson::first();

$rating = $lesson->getVoteBuilder()
                 ->user($user) // you may also use $user->id
                 ->uniqueVoteForUsers(true) // update if already rated
                 ->amount(3);

Update a vote

$lesson = Lesson::first();

$lesson->updateVote($vote_id, $amount); // vote_id and the new amount value
$lesson->updateVotesForUser($user_id, $amount); // update all votes for a single user related to the lesson

cancel a vote:

$lesson = Lesson::first();
$lesson->cancelVote($vote_id); // delete a vote with the giving id
$lesson->cancelVotesForUser($user_id); // delete all votes for a single user related to the lesson
$lesson->resetVotes(); // delete all rating related to the lesson

check if a model is already votes:

$lesson->isVoted();
$lesson->isUpVoted();
$lesson->isDownVoted();
$lesson->isUpVotedBy($user_id);// check if its already up voted by the given user
$lesson->isDownVotedBy($user_id);// check if its already up voted by the given user

Fetch the votes count:

$lesson->upVotesCount();
$lesson->downVotesCount();
$lesson->votesCount(); // get the votes count (up votes + down votes)

get list of users who voted a model (voters):

$lesson->voters()->get();
$lesson->voters()->where('name', 'like', '%yoeunes%')->get();
$lesson->voters()->orderBy('name')->get();

get count votes between by dates

$lesson->countVotesByDate('2018-02-03 13:23:03', '2018-02-06 15:26:06');
$lesson->countVotesByDate('2018-02-03 13:23:03');
$lesson->countVotesByDate(null, '2018-02-06 15:26:06');
$lesson->countVotesByDate(Carbon::now()->parse('01-04-2017'), Carbon::now()->parse('01-06-2017'));
$lesson->countVotesByDate(Carbon::now()->subDays(2));

other api methods:

Lesson::select('lessons.*')->orderByAverageUpVotes('asc')->get()
Lesson::select('lessons.*')->orderByAverageDownVotes('desc')->get()

Query relations

$ratings = $user->votes
$ratings = $user->votes()->where('id', '>', 10)->get()

date transformer

Because we all love having to repeat less, this package allows you to define date transformers. Let's say we are using the following code a lot: $lesson->countRatingsByDate(Carbon::now()->subDays(3)). It can get a little bit annoying and unreadable. Let's solve that!

If you've published the configuration file, you will see something like this:

'date-transformers' => [
    // 'past24hours' => Carbon::now()->subDays(1),
    // 'past7days'   => Carbon::now()->subWeeks(1),
    // 'past14days'  => Carbon::now()->subWeeks(2),
],

They are all commented out as default. To make them available, simply uncomment them. The provided ones are serving as an example. You can remove them or add your own ones.

'date-transformers' => [
    'past3days' => Carbon::now()->subDays(3),
],

We can now retrieve the rating count like this:

$lesson->countVotesByDate('past3days');

License

MIT

voteable's People

Contributors

parsingeye avatar yoeunes avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

parsingeye

voteable's Issues

Laravel 8 support

Hey, I love the thought and work you putted in this. But I feel it's a shame that it not support neither of Laravel 7 or 8.

It's kinda easy to support these versions by editing composer.json.

BTW I'm welling to PR this if you're welcoming them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.