mll-lab/laravel-comment

为您的出色Laravel项目提供的一款评论系统。

1.4.0 2024-03-28 09:06 UTC

This package is auto-updated.

Last update: 2024-08-28 10:11:34 UTC


README

Validate Code Coverage

Packagist Latest Stable Version

为您的出色Laravel项目提供的一款评论系统。

安装

通过Composer

composer require mll-lab/laravel-comment

发布配置和迁移文件,然后迁移comments

php artisan vendor:publish
php artisan migrate

CanComment特质添加到您的用户模型中

use Actuallymab\LaravelComment\CanComment;

final class User extends Model
{
    use CanComment;

Commentable接口和HasComments特质添加到您的可评论模型中

use Actuallymab\LaravelComment\Contracts\Commentable;
use Actuallymab\LaravelComment\HasComments;

final class Product extends Model implements Commentable
{
    use HasComments;

如果您想有自己的Comment模型,创建一个新模型并扩展Actuallymab\LaravelComment\Models\Comment

use Actuallymab\LaravelComment\Models\Comment as LaravelComment;

final class Comment extends LaravelComment

不要忘记在config/comment.php中更新模型类。

允许评分

final class Product extends Model implements Commentable
{
    use HasComments;

    public function canBeRated(): bool
    {
        return true; // default false
    }

要求评论经过审核

final class Product extends Model implements Commentable
{
    use HasComments;

    public function mustBeApproved(): bool
    {
        return true; // default false
    }

允许某些用户无需审核即可评论

final class User extends Model
{
    use CanComment;

    protected $fillable = [
        'isAdmin',
    ];

    public function canCommentWithoutApprove(): bool
    {
        return $this->isAdmin;
    }

用法

$user = User::firstOrFail();
$product = Product::firstOrFail();

// Pass the model to comment, the content and an optional rate
$user->comment($product, 'Lorem ipsum ..', 3);

// Only necessary if:
// - User::canCommentWithoutApprove() returns false
// - Product::mustBeApproved() returns true
$product->comments[0]->approve();

// Calculates the average rating of approved comments
$product->averageRate();

// Calculates the amount of approved comments
$product->totalCommentsCount();

提示:您可能想查看tests/CommentTest.php文件以检查所有可能的用法。

变更日志

本项目所有显著更改均记录在CHANGELOG.md中。

贡献

请参阅CONTRIBUTINGCONDUCT

安全

如果您发现任何安全问题,请通过电子邮件dev@mll.com联系,而不是使用问题跟踪器。

鸣谢