ghanem / rating

Laravel评分系统

V2.01 2020-12-15 15:42 UTC

This package is auto-updated.

Last update: 2024-09-10 06:18:58 UTC


README

Latest Stable Version License

Total Downloads

Laravel Rating

https://scontent-cai1-1.xx.fbcdn.net/v/t31.0-8/18192521_1536772739688541_5883708562629992092_o.jpg?oh=281577e64a1e326ff1989f047ab21df6&oe=59BAEBCA Laravel 5评分系统

安装

首先,通过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.

赞助

💚️成为赞助商