d35k / rating
Laravel 5 的评分系统
0.0.5
2017-11-02 01:01 UTC
Requires
- php: >=5.5.9
- illuminate/support: ~5.0
This package is not auto-updated.
Last update: 2024-09-20 09:16:32 UTC
README
Laravel 5 的评分系统
安装
首先,通过 Composer 拉取此包。
composer require d35k/rating
或者在项目的 composer.json 文件中添加此内容。
"require": {
"d35k/Rating": "^0",
}
然后,在 app/config/app.php
中包含服务提供者。
'providers' => [ d35k\Rating\RatingServiceProvider::class ];
入门
包安装正确后,需要生成迁移文件。
php artisan rating:migration
它将生成 <timestamp>_create_ratings_table.php
迁移文件。您现在可以使用 artisan migrate 命令运行它。
php artisan migrate
迁移后,将出现一个新表,名为 ratings
。
用法
设置模型
<?php namespace App; use d35k\Rating\Traits\Ratingable as Rating; use Illuminate\Database\Eloquent\Model; class Post extends Model implements Rating { use Rating; }
创建评分
$user = User::first(); $post = Post::first(); $rating = $post->rating([ 'rating' => 5, 'rating_question' => 'question', 'author_role' => 'role' ], $user); dd($rating);
创建或更新唯一的评分(适用于未知问题)
$user = User::first(); $post = Post::first(); $rating = $post->ratingUnique([ 'rating' => 5, 'rating_question' => 'question', 'author_role' => 'role' ], $user); dd($rating);
更新评分
$rating = $post->updateRating(1, [ 'rating' => 3 ]);
删除评分
$post->deleteRating(1);
获取评分总和
$post->sumRating // $post->sumRating() also works for this.
获取平均评分
$post->avgRating // $post->avgRating() also works for this.
获取过滤后的平均评分
$post->avgRatingByFilter('column', 'filter')
获取评分百分比。
这也是强制最大评分值的方法。
$post->ratingPercent $post->ratingPercent(10)); // Ten star rating system // Note: The value passed in is treated as the maximum allowed value. // This defaults to 5 so it can be called without passing a value as well.
计算正面评分数量
$post->countPositive // $post->countPositive() also works for this.
计算负面评分数量
$post->countNegative // $post->countNegative() also works for this.