百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rating

> 后端框架
开源

Laravel Eloquent Rating 允许您为任何模型分配评分。

189 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Laravel Eloquent Rating 允许您为任何模型分配评分。

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPassign-ratingseloquentlaravelmodel

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

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