Coder Social home page Coder Social logo

rating's Introduction

Laravel Eloquent Rating

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Laravel Eloquent Rating allows you to assign ratings to any model, just like any other review based on stars.

๐Ÿค Supporting

If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. ๐Ÿ“ฆ

๐Ÿš€ Installation

Install the package:

$ composer require rennokki/rating

Publish the config:

$ php artisan vendor:publish --provider="Rennokki\Rating\RatingServiceProvider" --tag="config"

Publish the migrations:

$ php artisan vendor:publish --provider="Rennokki\Rating\RatingServiceProvider" --tag="migrations"

Preparing the model

To allow a model to rate other models, it should use the CanRate trait and implement the Rater contract.

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

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

The other models that can be rated should use CanBeRated trait and Rateable contract.

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

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

If your model can both rate & be rated by other models, you should use Rate trait and Rating contract.

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

class User extends Model implements Rating
{
    use Rate;

    //
}

๐Ÿ™Œ Usage

To rate other models, simply call rate() method:

$page = Page::find(1);

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

As a second argument to the rate() method, you can pass the rating score. It can either be string, integer or float.

To update a rating, you can call updateRatingFor() method:

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

As you have seen, you can call averageRating() within models that can be rated. The return value is the average arithmetic value of all ratings as float.

If we leave the argument empty, we will get 0.00 because no other Page model has rated the page so far. But since users have rated the page, we will calculate the average only from the User models, since only they have voted the page, strictly by passing the class name as the argument.

$page = Page::find(1);

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

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

While in our example, the User class can both rate and be rated, we can leave the argument empty if we reference to its class:

$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

The relationships are based on this too:

$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

๐Ÿ› Testing

vendor/bin/phpunit

๐Ÿค Contributing

Please see CONTRIBUTING for details.

๐Ÿ”’ Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

๐ŸŽ‰ Credits

rating's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar laravel-shift avatar rennokki avatar semsphy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rating's Issues

Error Installing rating

macwin in ~/Downloads/robi-mylife-master/laravel > composer require rennokki/rating
Using version ^3.0 for rennokki/rating
./composer.json has been updated
Running composer update rennokki/rating
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

Problem 1
- rennokki/rating[3.0.0, ..., 3.0.1] require illuminate/database ^7.30|^8.23 -> found illuminate/database[v7.30.0, ..., v7.30.4, v8.23.1, ..., v8.42.1] but these were not loaded, likely because it conflicts with another require.
- Root composer.json requires rennokki/rating ^3.0 -> satisfiable by rennokki/rating[3.0.0, 3.0.1].

Error trying to get avg rating

Im tryging yo get avg rating from model Professional rated by a Client model, the rating record is saved in ratings table, but when i try to show the rating it shows me an error :
(url)

image

Any idea

Thanks for the help

Laravel 6 Support

Using version ^1.1 for rennokki/rating
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for rennokki/rating ^1.1 -> satisfiable by rennokki/rating[1.1.0].
- Conclusion: remove laravel/framework v6.0.3
- Conclusion: don't install laravel/framework v6.0.3
- rennokki/rating 1.1.0 requires laravel/framework ~5.5 -> satisfiable by laravel/framework[5.5.x-dev, 5.6.x-dev, 5.7.x-dev, 5.8.x-dev].
- Can only install one of: laravel/framework[5.5.x-dev, v6.0.3].
- Can only install one of: laravel/framework[5.6.x-dev, v6.0.3].
- Can only install one of: laravel/framework[5.7.x-dev, v6.0.3].
- Can only install one of: laravel/framework[5.8.x-dev, v6.0.3].
- Installation request for laravel/framework (locked at v6.0.3, required as ^6.0) -> satisfiable by laravel/framework[v6.0.3].

Installation failed, reverting ./composer.json to its original content.

Timestamp is not updated.

Hello,

How to reproduce

$page = Page::find(1);
$user->rate($page, 10);

\Rennokki\Rating\Models\RaterModel::first(); //created_at: null and updated_at: null,

Thanks,

The average rating returned not correct.

Am thinking the average rating should be a waited average. Not the average rating of total columns in the database. Something like:

โˆ‘๐‘›๐‘–=1๐‘ค๐‘–โ‹…๐‘Ž๐‘–
----------
โˆ‘๐‘›๐‘–=1๐‘ค๐‘–


1โ‹…5+2โ‹…3+3โ‹…1+4โ‹…17+5โ‹…2.         92 -       (Weighted sum)
-------------------------- = ---- 
5+3+1+17+2                    28      -  (Total sum)

typo error

in CanRate.php line: 80

if (! $this->hasRated($model)) {
return $this->rate($mode, $newRating);
}

Undefined variable: mode

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.