alfonsobries / laravel-commentable
一个用于给任何模型添加评论的Laravel扩展包
0.0.2
2022-10-10 19:24 UTC
Requires
- php: ^8.0
- laravel/framework: ^9.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.11
- orchestra/testbench: ^7.9
- pestphp/pest: ^1.22
- phpstan/phpstan: ^1.8
- rector/rector: ^0.14.5
README
一个用于给任何模型添加评论的Laravel扩展包
使用
安装
- 安装composer包
composer require alfonsobries/laravel-commentable
- 发布数据库迁移
php artisan vendor:publish --provider="Alfonsobries\LaravelCommentable\LaravelCommentableServiceProvider" --tag="migrations"
- 可选:发布配置文件
php artisan vendor:publish --provider="Alfonsobries\LaravelCommentable\LaravelCommentableServiceProvider" --tag="config"
- 使用
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; // ... }
- 将
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; // ... }
添加评论
- 使用
addComment
方法添加匿名评论
$blogPost = BlogPost::first(); $comment = $blogPost->addComment('my comment');
- 使用
addCommentFrom
方法添加来自用户(或实现CanCommentInterface
接口的模型)的评论
$user = User::first(); $blogPost = BlogPost::first(); $comment = $blogPost->addCommentFrom($user, 'my comment');
- 您还可以使用
comment
方法通过用户模型(或实现CanCommentInterface
接口的模型)进行评论。
$user = User::first(); $blogPost = BlogPost::first(); $comment = $user->comment($blogPost, 'my comment');
添加评论回复
-
Comment
方法是另一个可评论实例,这意味着您可以使用addComment
或addCommentFrom
方法添加回复。 -
您还可以使用
reply
和replyFrom
方法,它们是上述评论方法的别名。
$user = User::create([...]); $user2 = User::create([...]); $blogPost = BlogPost::first(); $comment = $blogPost->commentFrom($user, 'my comment'); $comment->replyFrom($user2, 'a reply');
- 使用
addCommentFrom
方法添加来自用户(或实现CanCommentInterface
接口的模型)的评论
$user = User::first(); $blogPost = BlogPost::first(); $comment = $blogPost->addCommentFrom($user, 'my comment');
- 您还可以使用
comment
方法通过用户模型(或实现CanCommentInterface
接口的模型)进行评论。
$user = User::first(); $blogPost = BlogPost::first(); $comment = $user->comment($blogPost, 'my comment');
获取评论
- 您可以使用
comments
方法获取所有用户评论
$comments = User::first()->comments();
- 您可以使用
comments
方法获取与可评论模型关联的所有评论
$comments = BlogPost::first()->comments();
排序评论
使用 popular
和 unpopular
范围方法按受欢迎程度(平均正面点赞数)对评论进行排序。
$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();
您可以使用 approved
和 notApproved
方法过滤已批准或未批准的评论。
$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 联系表单联系。所有安全漏洞都将得到及时处理。
鸣谢
此项目得益于所有 贡献者。