alfonsobries/laravel-commentable

一个用于给任何模型添加评论的Laravel扩展包

0.0.2 2022-10-10 19:24 UTC

This package is auto-updated.

Last update: 2024-09-16 19:33:56 UTC


README

一个用于给任何模型添加评论的Laravel扩展包

Stable Version License PHP Version Require

使用

安装

  1. 安装composer包
composer require alfonsobries/laravel-commentable
  1. 发布数据库迁移
php artisan vendor:publish --provider="Alfonsobries\LaravelCommentable\LaravelCommentableServiceProvider" --tag="migrations"
  1. 可选:发布配置文件
php artisan vendor:publish --provider="Alfonsobries\LaravelCommentable\LaravelCommentableServiceProvider" --tag="config"
  1. 使用 CommentableInterface 接口和 Commentable 特性配置将要接收评论的模型。
<?php

namespace App\Models;

use Alfonsobries\LaravelCommentable\Contracts\CommentableInterface;
use Alfonsobries\LaravelCommentable\Traits\Commentable;
use Illuminate\Database\Eloquent\Model;


class BlogPost extends Model implements CommentableInterface
{
    use Commentable;
    // ...
}
  1. CanCommentInterface 接口和 CanComment 特性添加到将要添加评论的模型中(通常是 User 模型)。由于您可以添加匿名评论,这只有在您想接受用户评论时才是必需的。
<?php

namespace App\Models;

use Alfonsobries\LaravelCommentable\Contracts\CanCommentInterface;
use Alfonsobries\LaravelCommentable\Traits\CanComment;
use Illuminate\Database\Eloquent\Model;


class User extends Model implements CanCommentInterface
{
    use CanComment;
    // ...
}

添加评论

  1. 使用 addComment 方法添加匿名评论
$blogPost = BlogPost::first();

$comment = $blogPost->addComment('my comment');
  1. 使用 addCommentFrom 方法添加来自用户(或实现 CanCommentInterface 接口的模型)的评论
$user = User::first();
$blogPost = BlogPost::first();

$comment = $blogPost->addCommentFrom($user, 'my comment');
  1. 您还可以使用 comment 方法通过用户模型(或实现 CanCommentInterface 接口的模型)进行评论。
$user = User::first();
$blogPost = BlogPost::first();

$comment = $user->comment($blogPost, 'my comment');

添加评论回复

  1. Comment 方法是另一个可评论实例,这意味着您可以使用 addCommentaddCommentFrom 方法添加回复。

  2. 您还可以使用 replyreplyFrom 方法,它们是上述评论方法的别名。

$user = User::create([...]);
$user2 = User::create([...]);
$blogPost = BlogPost::first();

$comment = $blogPost->commentFrom($user, 'my comment');
$comment->replyFrom($user2, 'a reply');
  1. 使用 addCommentFrom 方法添加来自用户(或实现 CanCommentInterface 接口的模型)的评论
$user = User::first();
$blogPost = BlogPost::first();

$comment = $blogPost->addCommentFrom($user, 'my comment');
  1. 您还可以使用 comment 方法通过用户模型(或实现 CanCommentInterface 接口的模型)进行评论。
$user = User::first();
$blogPost = BlogPost::first();

$comment = $user->comment($blogPost, 'my comment');

获取评论

  1. 您可以使用 comments 方法获取所有用户评论
$comments = User::first()->comments();
  1. 您可以使用 comments 方法获取与可评论模型关联的所有评论
$comments = BlogPost::first()->comments();

排序评论

使用 popularunpopular 范围方法按受欢迎程度(平均正面点赞数)对评论进行排序。

$popularComments = BlogPost::first()->comments()->popular()->get();

$unpopularComments = BlogPost::first()->comments()->unpopular()->get();

更新评论

由于 Comment 对象只是一个常规的Eloquent模型,您可以使用不同的方法来更新模型。

$comment->update(['comment' => 'updated comment']);

批准评论

默认情况下,所有新评论都是未批准的(approved_at=null),这意味着您需要根据具体需求手动批准它们(您可以根据以下列出的事件添加事件监听器来完成此操作)。如果您不需要处理已批准和未批准的评论,您可以在查询评论时简单地忽略过滤器。

$comment->approve();
$comment->unapprove();

您可以使用 approvednotApproved 方法过滤已批准或未批准的评论。

$model->comments()->approved()->get();
$model->comments()->notApproved()->get();

事件

Comment 模型触发以下事件,您可以监听这些事件

  • CommentCreated
  • CommentCreating
  • CommentUpdated
  • CommentUpdating
  • CommentDeleted
  • CommentDeleting
  • CommentSaved
  • CommentSaving

开发

使用 phpstan 分析代码

composer analyse

使用 php rector 代码重构

composer refactor

使用 php-cs-fixer 格式化代码

composer format

运行测试

composer test

安全

如果您在此包中发现安全漏洞,请通过 https://alfonsobries.com 联系表单联系。所有安全漏洞都将得到及时处理。

鸣谢

此项目得益于所有 贡献者

许可证

MIT © Alfonso Bribiesca