PHP code example of yoeunes / voteable

1. Go to this page and download the library: Download yoeunes/voteable 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/ */

    

yoeunes / voteable example snippets


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

$ php artisan migrate



namespace App;

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

class Lesson extends Model
{
    use Voteable;
}

$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);

$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

$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

$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

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

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

$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));

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

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

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

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

$lesson->countVotesByDate('past3days');