mll-lab/ laravel-comment
为您的出色Laravel项目提供的一款评论系统。
1.4.0
2024-03-28 09:06 UTC
Requires
- php: ^7.1 || ^8
- illuminate/contracts: ~5.5.0 || ~5.6.0 || ~5.7.0 || ~5.8.0 || ^6 || ^7 || ^8 || ^9 || ^10 || ^11
- illuminate/support: ~5.5.0 || ~5.6.0 || ~5.7.0 || ~5.8.0 || ^6 || ^7 || ^8 || ^9 || ^10 || ^11
Requires (Dev)
- ergebnis/composer-normalize: ^2
- fakerphp/faker: ^1.8
- orchestra/testbench: ~3.5.0 || ~3.6.0 || ~3.7.0 || ~3.8.0 || ~3.9.0 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9
- phpunit/phpunit: ^7.5 || ^8.4 || ^9 || ^10 || ^11
README
为您的出色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
中。
贡献
请参阅CONTRIBUTING和CONDUCT。
安全
如果您发现任何安全问题,请通过电子邮件dev@mll.com联系,而不是使用问题跟踪器。