PHP code example of yoeunes / rateable

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


'providers' => [
    ...
    Yoeunes\Rateable\RateableServiceProvider::class
    ...
];

$ php artisan migrate



namespace App;

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

class Lesson extends Model
{
    use Rateable;
}

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

$rating = $lesson->getRatingBuilder()
                 ->user($user) // you may also use $user->id
                 ->uniqueRatingForUsers(true) // update if already rated
                 ->rate(3);

$lesson = Lesson::first();

$lesson->updateRating($rating_id, $value); // rating_id and the new rating value
$lesson->updateRatingForUser($user_id, $value); // update all rating for a single user related to the lesson

$lesson = Lesson::first();
$lesson->deleteRating($rating_id); // delete a rating with the giving id
$lesson->deleteRatingsForUser($user_id); // delete all rating for a single user related to the lesson
$lesson->resetRating(); // delete all rating related to the lesson

$lesson->isRated();
$lesson->isRatedBy($user_id);// check if its already rated by the given user

$lesson->averageRating(); // get the average rating 
$lesson->averageRatingForUser($user_id); // get the average rating for a single user

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

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

$lesson->countRating()
$lesson->countRatingForUser($user_id)

$lesson->totalRating()
$lesson->totalRatingForUser($user_id)

$lesson->ratingPercentage()
$lesson->positiveRatingCount()
$lesson->positiveRatingTotal()
$lesson->negativeRatingCount()
$lesson->negativeRatingTotal()

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

$ratings = $user->ratings
$ratings = $user->ratings()->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->countRatingsByDate('past3days');