Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rating

> 后端框架
Open source

Laravel Eloquent Rating allows you to assign ratings to any model.

189 stars0 likes0 views
WebsiteGitHub

About

Laravel Eloquent Rating allows you to assign ratings to any model.

Laravel Eloquent Rating

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:

bash
$ composer require rennokki/rating

Publish the config:

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

Publish the migrations:

bash
$ 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.

php
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.

php
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.

php
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:

php
$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:

php
$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.

php
$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:

php
$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:

php
$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

bash
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

  • Alex Renoki
  • All Contributors

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPassign-ratingseloquentlaravelmodel

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架