blok/laravel-commentable

适用于Laravel 5的评论辅助包

1.2.1 2022-05-25 10:43 UTC

This package is auto-updated.

Last update: 2024-09-25 16:27:53 UTC


README

Build Status PHP from Packagist Latest Version License

安装

使用 Composer 在项目的根目录下安装此包。

$ composer require blok/laravel-commentable

要开始使用,您需要发布供应商资产并迁移

php artisan vendor:publish --provider="Blok\Commentable\CommentableServiceProvider" && php artisan migrate

用法

设置模型

<?php

namespace App;


use Blok\Commentable\Traits\HasComments;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasComments;
}

创建评论

$user = User::first();
$post = Post::first();

$comment = $post->comment([
    'title' => 'Some title',
    'body' => 'Some body',
], $user);

dd($comment);

将评论作为另一个评论的子评论创建(例如,一个答案)

$user = User::first();
$post = Post::first();

$parent = $post->comments->first();

$comment = $post->comment([
    'title' => 'Some title',
    'body' => 'Some body',
], $user, $parent);

dd($comment);

更新评论

$comment = $post->updateComment(1, [
    'title' => 'new title',
    'body' => 'new body',
]);

删除评论

$post->deleteComment(1);

计算实体拥有的评论数量

$post = Post::first();

dd($post->commentCount());

测试

$ phpunit

安全

如果您在此包中发现安全漏洞,请发送电子邮件至 hello@brianfaust.me。所有安全漏洞都将得到及时处理。

致谢

许可证

MIT © Brian Faust