devianl2/laravel-comment

Laravel评论和可评分包

1.0.6 2023-05-13 16:16 UTC

This package is auto-updated.

Last update: 2024-09-13 19:14:18 UTC


README

此Laravel包为Laravel应用程序中管理评论和评分提供了强大的解决方案。它允许您轻松执行以下操作:

安装

您可以通过运行以下命令使用Composer安装此包:

composer require devianl2/laravel-comment

使用方法

要使用此包,请按照以下步骤操作:

  1. 设置包:安装后,请确保发布包配置和迁移。
php artisan vendor:publish --provider="Devianl2\CommentRateable\CommentRateableServiceProvider" --tag="migrations"
php artisan migrate
  1. 将特性添加到您的Post模型:为了启用评论和评分功能,将CommentRateable特性添加到您的Post模型。
use Devianl2\CommentRateable\Traits\CommentRateable;

class Post extends Model
{
    use CommentRateable;
}
  1. 执行操作:现在您可以使用Post模型上可用的方法执行操作,例如检索评论、获取平均评分或删除评论。

用例

保存评论

$post = Post::first();
$comment = $post->comment([
    'title' => 'This is a test title',
    'body' => 'And we will add some shit here',
    'rating' => 5,
    'is_approved' => true, // This is optional and defaults to false
], $user);

更新评论

$post = Post::first();
$comment = $post->updateComment(
    $commentId,
    [
        'title' => 'This is a test title',
        'body' => 'And we will add some shit here',
        'rating' => 5,
        'is_approved' => true, // This is optional and defaults to false
    ], $user);

其他方法

  1. 获取平均评分:检索文章的平均评分。
$post = Post::first();
$post->averageRating();
  1. 获取评论:检索文章的所有评论。
$post->getComments();
  1. 获取已批准评论:仅检索文章的已批准评论。
$post->getComments(true);
  1. 获取分页的已批准评论:分页检索文章的已批准评论。
$post->getComments(true, true);
  1. 获取所有评论(无分页):无分页和限制地检索文章的所有评论。(将限制设置为0以获取无限评论)
$post->getComments(true, false, 0);
  1. 删除评论:删除评论。
$comment->delete();

贡献

欢迎对此包的贡献。如果您发现任何问题或想添加新功能,请在此包的GitHub仓库中创建问题或提交拉取请求。

许可证

此包是开源软件,根据MIT许可证许可。