kivicms/laravel-review-rateable

Laravel 8的评论与评分系统

2.0.0 2022-04-03 15:28 UTC

This package is auto-updated.

Last update: 2024-09-30 01:48:55 UTC


README

laravel 5, 6, 7 & 8的评论可评分系统。您可以通过以下方式对模型进行评分:

  • 总体评分
  • 客户服务评分
  • 质量评分
  • 友好度评分
  • 价格评分

您还可以设置评分的模型是否被推荐。

安装

首先,通过Composer拉取该包。

composer require kivicms/laravel-review-rateable

然后,在app/config/app.php中包含服务提供者。注意:如果您正在运行Laravel 5.5+,这将为您自动加载。

'providers' => [
    Kivicms\ReviewRateable\ReviewRateableServiceProvider::class
];

发布配置文件

php artisan vendor:publish --provider="Kivicms\ReviewRateable\ReviewRateableServiceProvider" --tag="config"

最后,您需要发布并运行迁移。

php artisan vendor:publish --provider="Kivicms\ReviewRateable\ReviewRateableServiceProvider" --tag="migrations"

运行迁移

php artisan migrate

设置模型

<?php

namespace App;

use Kivicms\ReviewRateable\Contracts\ReviewRateable;
use Kivicms\ReviewRateable\Traits\ReviewRateable as ReviewRateableTrait;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements ReviewRateable
{
    use ReviewRateableTrait;
}

创建评分

在创建评分时,您可以通过在数组中添加“approved”来指定评分是否已批准。这是可选的,如果省略,则默认为未批准,以便在发布前进行审查。

$user = User::first();
$post = Post::first();

$rating = $post->rating([
    'title' => 'This is a test title',
    'body' => 'And we will add some shit here',
    'customer_service_rating' => 5,
    'quality_rating' => 5,
    'friendly_rating' => 5,
    'pricing_rating' => 5,
    'rating' => 5,
    'recommend' => 'Yes',
    'approved' => true, // This is optional and defaults to false
], $user);

dd($rating);

更新评分

$rating = $post->updateRating(1, [
    'title' => 'new title',
    'body' => 'new body',
    'customer_service_rating' => 1,
    'quality_rating' => 1,
    'friendly_rating' => 3,
    'pricing_rating' => 4,
    'rating' => 4,
    'recommend' => 'No',
    'approved' => true, // This is optional and defaults to false
]);

标记评论为已批准

$rating = $post->updateRating(1, ['approved' => true]);

删除评分

$post->deleteRating(1);

获取特定资源的已批准或未批准的评论/评分

// Get approved ratings
$ratings = $post->getApprovedRatings($post->id, 'desc');

// Get not approved ratings
$ratings = $post->getNotApprovedRatings($post->id, 'desc');

// Get all ratings whether approved or not
$ratings = $post->getAllRatings($post->id, 'desc');

// Get the most recent ratings (limit and sort are optional)
// Limit default is 5, sort default is desc
$ratings = $post->getRecentRatings($post->id, 5, 'desc');

// Get the most recent user ratings (limit and sort are optional)
// Limit default is 5, approved default is true, sort default is desc
$userRatings = $post->getRecentUserRatings($user->id, 5, true, 'desc');

获取平均评分

// Get Overall Average Rating
$post->averageRating()

// Get Customer Service Average Rating
$post->averageCustomerServiceRating()

// Get Quality Average Rating
$post->averageQualityRating()

// Get Friendly Average Rating
$post->averageFriendlyRating()

// Get Price Average Rating
$post->averagePricingRating()

$post->averageRating(2) //round to 2 decimal place
$post->averageRating(null, true) //get only approved average rating 

获取所有评分

$post = Post::first();

$ratings = $post->getAllRatings($post->id);

计算总评分

$post->countRating()

获取评分百分比。

这也是强制最大评分值的方法。

$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.

注意

这是从Trexology的原始代码 - laravel-reviewRateable分支出来的。

请注意,此代码在原始代码中没有使用,并且没有得到维护。