digikraaft/laravel-review-rating

Laravel的评论与评分系统

v3.1.0 2024-06-05 05:32 UTC

This package is auto-updated.

Last update: 2024-09-05 06:06:46 UTC


README

tests Build Status Scrutinizer Code Quality Code Intelligence Status License: MIT

Laravel的评论与评分系统

此包为Laravel提供了一个简单的评论与评分系统。它支持Laravel 5.8及其以上版本。以下是它的快速演示:

//create a review
$author = User::find(1);
$review = "Awesome package! I highly recommend it!!";

$model->review($review, $author);

//write a review and include a rating
$model->review($review, $author, 5);

//write a review and include a rating and a title
$model->review($review, $author, 5, "Lovely packages");

//get the last review
$model->latestReview(); //returns an instance of \Digikraaft\ReviewRating\Review

//get the review content of the last review
$model->latestReview()->review; //returns 'Awesome package! I highly recommend it!!'

//get the rating of the last review
$model->latestReview()->rating; //return 5

//get the title of the last review
$model->latestReview()->title; //returns 'Lovely packages'

安装

您可以通过composer安装此包

composer require digikraaft/laravel-review-rating

您必须使用以下命令发布迁移

php artisan vendor:publish --provider="Digikraaft\ReviewRating\ReviewRatingServiceProvider" --tag="migrations"

运行迁移以发布reviews表,使用以下命令:

php artisan migrate

您可以选择使用以下命令发布配置文件

php artisan vendor:publish --provider="Digikraaft\ReviewRating\ReviewRatingServiceProvider" --tag="config"

将要发布到config/review-rating.php的文件内容

return [
    /*
      * The class name of the review model that holds all reviews.
      *
      * The model must be or extend `Digikraaft\ReviewRating\Review`.
      */
    'review_model' => Digikraaft\ReviewRating\Models\Review::class,

    /*
     * The name of the column which holds the ID of the model related to the reviews.
     *
     * Only change this value if you have set a different name in the migration for the reviews table.
     */
    'model_primary_key_attribute' => 'model_id',

];

使用方法

HasReviewRating特质添加到模型中

use Digikraaft\ReviewRating\Traits\HasReviewRating;
use Illuminate\Database\Eloquent\Model;

class EloquentModel extends Model
{
    use HasReviewRating;
}

创建评论

要创建评论,请使用特质的review函数,如下所示:

$author = User::find(1);
$review = "Awesome package! I highly recommend it!!";

$model->review($review, $author);

第一个参数是评论内容,第二个参数是作者。这可以是任何Eloquent模型。

要创建带评分的评论,请将评分值作为review函数的第三个参数传入。有效的值是intfloat

$author = User::find(1);
$review = "Awesome package! I highly recommend it!!";

$model->review($review, $author, 5);

要创建带评分和标题的评论,请将标题作为review函数的第四个参数添加。

$author = User::find(1);
$review = "Awesome package! I highly recommend it!!";

$model->review($review, $author, 5, "Lovely packages");

您还可以使用hasReviewed函数检查用户是否已对模型进行过评论。

    if ($model->hasReviewed(auth()->user())) {
        // user has reviewed the model
    }

检索评论

您可以通过以下方式获取最后一条评论:

$model->latestReview(); //returns the latest instance of Digikraaft\ReviewRating\Review

可以通过以下方式获取评论内容:

$model->latestReview()->review;

要获取评论的评分,请这样做:

$model->latestReview()->rating;

要获取评论的标题,请这样做:

$model->latestReview()->title;

可以通过以下方式获取所有评论:

$model->reviews;

要访问检索到的每条评论,请这样做:

$reviews = $model->reviews;

foreach($reviews as $review){
    echo $review->review . "<br>";
}

可以使用allReviews作用域检索模型所有实例的所有评论。

$allReviews = EloquentModel::allReviews();

检索基本的评论统计信息

您可以获取模型有多少条评论:

$model->numberOfReviews();

要获取模型在一段时间内收到的评论数量,请传入格式为Carbon$from$to日期作为第一个和第二个参数。

//get the number of reviews a model has received over the last month
$from = now()->subMonth();
$to = now();

$model->numberOfReviews($from, $to);

注意,如果$from日期晚于$to日期,将抛出InvalidDate异常。

您可以获取模型的评分数量:

$model->numberOfRatings();

要获取模型在一段时间内收到的评分数量,请传入格式为Carbon$from$to日期作为第一个和第二个参数。

//get the number of reviews a model has received over the last month
$from = now()->subMonth();
$to = now();

$model->numberOfRatings($from, $to);

要获取模型的平均评分

$model->averageRating();

返回的平均评分默认不进行四舍五入。如果您想对返回结果进行四舍五入,请传入您想要四舍五入到的小数位数整数。

//round up to 2 decimal places
$model->averageRating(2);

要获取模型在一段时间内的平均评分,请传入格式为Carbon$from$to日期作为第一个和第二个参数。

//get the average rating a model has received over the last month, rounded to 2 decimal places:
$from = now()->subMonth();
$to = now();

$model->averageRating(2, $from, $to);

可以使用withRatings作用域检索模型所有实例的所有带评分评论。

$allReviewsWithRating = EloquentModel::withRatings();

检查模型是否有评论

您可以检查模型是否至少有一条评论。

$model->hasReview();

检查模型是否有评分

您可以检查模型是否至少有一个评分。

$model->hasRating();

事件

当创建评论时,将触发Digikraaft\ReviewRating\Events\ReviewCreatedEvent事件。您可以监听此事件并采取必要的操作。评论的实例将传递给事件类,并可用于使用。

namespace Digikraaft\ReviewRating\Events;

use Digikraaft\ReviewRating\Models\Review;
use Illuminate\Database\Eloquent\Model;

class ReviewCreatedEvent
{
    /** @var \Digikraaft\ReviewRating\Models\Review */
    public Review $review;

    public function __construct(Review $review)
    {
        $this->review = $review;
    }
}

自定义模型和迁移

您可以通过在review-rating配置文件的review_model键中指定不同的类名来更改所使用的模型。

当使用自定义迁移时,您还可以更改reviews表中使用的列名(默认为model_id)。如果是这种情况,请同时更改review-rating配置文件中的model_primary_key_attribute键。

测试

使用以下命令运行您的测试

composer test

更多精彩内容

请访问此处获取更多免费精彩内容!

更新日志

请参阅更新日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全

如果您发现任何安全相关的问题,请通过电子邮件hello@digikraaft.ng而不是使用问题跟踪器来报告。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件