dorvidas / laravel-ratings
一个简单的Laravel包,用于轻松地对用户、帖子或其他模型进行评分。您甚至可以为用户在模型中的角色(例如,帖子作者或插画师)分配评分。
Requires
- php: >=7.0
Requires (Dev)
- orchestra/database: ^3.5
- orchestra/testbench: ^3.5
- phpunit/phpunit: ^6.5
README
轻松地对用户、帖子或其他模型进行评分。您甚至可以为用户在模型中的角色(例如,帖子作者或插画师)分配评分。
安装
Lumen
您可以使用以下命令通过composer安装此包:
composer require dorvidas/laravel-ratings
将服务提供者和配置添加到 bootstrap/app.php
$app->register(\Dorvidas\Ratings\RatingsServiceProvider::class); $app->configure('ratings');
Laravel
您可以使用以下命令通过composer安装此包:
composer require dorvidas/laravel-ratings
添加服务提供者
Dorvidas\Ratings\RatingsServiceProvider::class,
发布供应商文件
php artisan vendor:publish --tag=public --force
用法
分配评分
为了对模型进行评分,您需要访问 [RatingBuilder](#评分构建器API) 实例。这可以通过几种方式完成
- 手动创建实例
$builder = new \Dorvidas\Ratings\RatingBuilder; $builder->model($model)->give(5);
- 通过外观
Rate::model($model)->give(5);
- 通过
\Dorvidas\Ratings\RateableTrait特性方法
$post->rate()->give(5);
评分构建器API
model($model)- 设置评分的对象。当通过特性使用时,这可以省略,因为我们已经知道要评分的模型。give(5)- 设置评分。返回值是\Dorvidas\Ratings\Models\Rating模型。by($user)- 设置评分用户。如果跳过,则使用授权用户。on($model)- 这用于定义用户被评分的原因。用Laravel术语来说,我们定义我们在哪个模型上评分用户。以下示例为帖子评分用户
$post = Post::first(); $user = User::first(); $user->rate()->on($post)->give(5);
as($role)- 这用于定义用户的角色。用Laravel术语来说,我们定义哪个模型的列包含用户ID。如果省略,则默认为user_id。一个现实生活中的例子可能是为帖子作者和插画师分配不同的评分
$author = User::first(); $illustrator = User::skip(1)->first(); $author->rate()->on($post)->as('author_id')->give(5); $illustrator()->rate()->on($post)->as('illustrator_id')->give(5);
获取评分
评分通过 \Dorvidas\Ratings\Rating 模型检索。可以通过查询模型上的 ratings 关系来检索模型评分。关系是多态的。不要忘记添加 \Dorvidas\Ratings\Models\RateableTrait 以使其正常工作。
评分模型字段
model- 被评分模型的名称。model_id- 被评分模型的ID。on_model- 这通常用于我们评分用户时,想要定义我们为什么(在哪个模型上)评分他,例如在 "帖子" 上。on_model_id- 我们评分的模型的ID。如果我们对 "帖子" 进行评分,这将是指帖子的ID。on_model_column- 在使用模型评分时,我们使用此字段来定义哪个列定义用户ID。默认为user_id。rated_by- 评分用户的ID。rating- 实际评分。
评分模型查询作用域
of- 过滤特定模型的评分。通过model和model_id列进行过滤。on- 过滤特定模型上的评分。通过on_model和on_model_id列进行过滤。as- 通过角色过滤评分。通过on_model_column列进行过滤。model- 通过model列进行过滤。modelId- 通过model_id列进行过滤。onModel- 通过on_model列进行过滤。onModelId- 通过on_model_id列进行过滤。
示例
获取 "帖子" 评分列表的基本示例
$post = Post::first(); $ratings = Rating::of($post)->get();
如果没有模型对象,您可以通过作用域 modelId 和 model 传递模型 ID 和模型类型。
$postId = 1; $ratings = Rating::model(Post::class)->modelId($modelId)->get(); //Or doing where statements $ratings = Rating::where('model', Post::class)->where('model_id, $postId)->get();
如果我们想要获取用户在特定模型上的评分
$user = User::first(); $post = Post::first(); $user->ratings()->on($post)->first(); //Returns Rating model instance $user->ratings()->on($post)->first()->rating; //Returns actual rating
如果我们想要获取特定角色在特定模型上的用户评分
$user = User::first(); $post = Post::first(); $user->ratings()->on($post)->as('author_id')->first();
获取评分聚合
评分聚合存储在表 rating_aggregates 中。如果类使用了特质 RateableTrait,您可以通过查询多态关系来获取聚合。
$model->rating_aggregates;
每当创建一个评分时,会触发一个 \Dorvidas\Ratings\Events\RatingCreatedEvent 事件。还有一个监听器 \Dorvidas\Ratings\Listeners\RecalculateRatingAggregatesListener 用于更新聚合条目。所以请不要忘记注册这些事件和监听器。
配置
database_prefix- 允许添加数据库前缀。