ghanem / rating
Laravel评分系统
V2.01
2020-12-15 15:42 UTC
Requires
- php: ^7.4|^8.0
- illuminate/support: ^5.0|^7.0|^8.0
README
Laravel Rating
安装
首先,通过Composer引入该包。
composer require ghanem/rating
或者在项目的composer.json文件中添加以下内容。
"require": {
"Ghanem/Rating": "1.*",
}
然后在app/config/app.php
中包含服务提供者。
'providers' => [ Ghanem\Rating\RatingServiceProvider::class ];
入门指南
包正确安装后,您需要生成迁移文件。
php artisan rating:migration
它将生成<timestamp>_create_ratings_table.php
迁移文件。您现在可以使用Artisan migrate命令运行它
php artisan migrate
迁移后,将出现一个新表ratings
。
用法
设置模型
<?php namespace App; use Ghanem\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 ], $user); dd($rating);
创建或更新唯一的评分
$user = User::first(); $post = Post::first(); $rating = $post->ratingUnique([ 'rating' => 5 ], $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->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.