adiliogobira / laravel-review-rateable
Laravel的评论与评分系统
dev-main
2021-02-27 13:58 UTC
Requires
- php: ^7.2
Requires (Dev)
- orchestra/testbench: ~3.5.0|~3.6.0|^4.0
- phpunit/phpunit: ^6.3|^7.0|^8.0
This package is auto-updated.
Last update: 2024-09-27 21:23:09 UTC
README
laravel 5 和 6 的Review Rateable系统。您可以通过以下方式对模型进行评分
- 总体评分
- 客户服务评分
- 质量评分
- 友好度评分
- 价格评分
您还可以设置被评分的模型是否推荐。
安装
首先,通过Composer拉取此包。
composer require sundarocs/laravel-review-rateable
然后,在app/config/app.php
中包含服务提供者。注意:如果您正在运行Laravel 5.5+,这将为您自动加载。
'providers' => [
Sumantablog\ReviewRateable\ReviewRateableServiceProvider::class
];
最后,您需要发布并运行迁移。
php artisan vendor:publish --provider="Sumantablog\ReviewRateable\ReviewRateableServiceProvider" --tag="migrations"
运行迁移
php artisan migrate
设置模型
<?php namespace App; use Sumantablog\ReviewRateable\Contracts\ReviewRateable; use Sumantablog\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 分叉而来。
请注意,此代码未在原始版本中使用,并且未得到维护。