PHP code example of rennokki / rating

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

    

rennokki / rating example snippets


use Rennokki\Rating\Traits\CanRate;
use Rennokki\Rating\Contracts\Rater;

class User extends Model implements Rater
{
    use CanRate;
    ...
}

use Rennokki\Rating\Traits\CanBeRated;
use Rennokki\Rating\Contracts\Rateable;

class User extends Model implements Rateable
{
    use CanBeRated;
    ...
}

use Rennokki\Rating\Traits\Rate;
use Rennokki\Rating\Contracts\Rating;

class User extends Model implements Rating
{
    use Rate;

    //
}

$page = Page::find(1);

$user->rate($page, 10);
$user->hasRated($page); // true
$page->averageRating(User::class); // 10.0, as float

$user->updateRatingFor($page, 9);
$page->averageRating(User::class); // 9.00, as float

$page = Page::find(1);

$user1->rate($page, 10);
$user2->rate($page, 6);

$page->averageRating(); // 0.00
$page->averageRating(User::class); // 8.00, as float

$user = User::find(1);

$user1->rate($user, 10);
$user2->rate($user, 6);

$user->averageRating(); // 8.00, as float
$user->averageRating(User::class); // 8.00, it is equivalent

$page->raters()->get(); // Pages that have rated this page
$page->raters(User::class)->get(); // Users that have rated this page

$user->ratings()->get(); // Users that this user has rated
$user->ratings(Page::class)->get(); // Pages that this user has rated