nazaryuzhyn / laravel-comments
Laravel应用程序的评论
v1.1.0
2023-04-29 08:44 UTC
Requires
- php: ^8.0|^8.1|^8.2
- illuminate/support: ^8.0|^9.0|^10.0
- laravel/framework: ^8.0|^9.0|^10.0
Requires (Dev)
- illuminated/testing-tools: ^8.1|^9.1
- orchestra/testbench: ^3.6|^5.0|^6.0|^7.0|^8.0
- phpunit/phpunit: ^7.0|^8.0|9.2.*|^9.5.10
README
Laravel的评论包。
功能
- 简单易用的安装包
- 创建评论
- 审核评论
- 自动审核评论
- 检索评论
安装
您可以通过composer安装此包
composer require nazaryuzhyn/laravel-comments
发布迁移
将迁移添加到您的项目中
php artisan vendor:publish --provider="Comments\CommentsServiceProvider" --tag=migrations
迁移发布后,您可以通过运行迁移来创建评论表
php artisan migrate
发布包配置
在您的终端中输入
php artisan vendor:publish --provider="Comments\CommentsServiceProvider" --tag=config
此命令将发布配置文件 config/comments.php
。
有关更多信息,请参阅完整配置文件。
注册包
在 config/app.php
中的 providers
数组中注册包服务提供者
'providers' => [ // ... 'Comments\CommentsServiceProvider', ],
用法
在模型中启用包
要使模型能够接收评论,请将 Comments\Traits\Commentable
特性添加到模型中
namespace App\Models; use Comments\Traits\Commentable; use Illuminate\Database\Eloquent\Model; class Article extends Model { use Commentable; // ... }
创建评论
要为您的模型创建评论,您可以使用 comment
方法。它接收您想要存储的评论字符串。
$article = Article::find(1); $comment = $article->comment('This is a comment...');
评论方法返回新创建的评论类。
有时您可能还希望代表其他用户创建评论。您可以使用 commentFromUser
方法,并将您的用户模型传递以与该评论绑定
$user = User::find(1); $article = Article::find(1); $comment = $article->commentFromUser($user, 'This is a comment from someone else.');
审核评论
默认情况下,您创建的所有评论都没有获得批准。
要批准单个评论,您可以在Comment模型上使用 approve
方法,如下所示
$article = Article::find(1); $comment = $article->comments->first(); $comment->approve();
自动审核评论
如果您想自动批准特定用户的评论,可以让您的模型实现以下接口和属性
namespace App\Models; use Comments\Contracts\Commentator; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements Commentator { use Commenting; protected bool $autoCommentApproval = true; // ... }
autoCommentApproval属性用于指定是否自动批准评论,您可以选择返回true以标记评论为已批准或返回false以标记评论为未批准。
检索评论
使用Commentable
特性的模型可以通过comments
关系访问其评论
$article = Article::find(1); // Retrieve all comments $comments = $article->comments; // Retrieve only approved comments $approved = $article->comments()->approved()->get(); // Retrieve only disapproved comments $disapproved = $article->comments()->disapproved()->get(); // Retrieve only for today comments $commentsForToday = $article->comments()->forToday()->get(); // Retrieve only before today comments $commentsBeforeToday = $article->comments()->beforeToday()->get();
测试
composer test
变更日志
请参阅CHANGELOG以获取有关最近更改的更多信息。
许可
MIT许可(MIT)。请参阅许可文件以获取更多信息。