namest/commentable

v0.2.2 2015-03-01 23:11 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:12:36 UTC


README

提供一种优雅的方式来与模型之间的评论功能交互。

注意:该包仅支持 Laravel 5

安装

步骤 1:安装包

composer require namest/commentable

步骤 2:在您的 config/app.php 文件中注册服务提供者

return [
    ...
    'providers' => [
        ...
        'Namest\Commentable\CommentableServiceProvider',
    ],
    ...
];

步骤 3:发布包资源,包括:配置、迁移。打开您的终端并输入

php artisan vendor:publish --provider="Namest\Commentable\CommentableServiceProvider"

步骤 4:迁移已发布的迁移

php artisan migrate

步骤 5:使用一些特质来制作令人惊叹的事情

class User extends Model
{
    use \Namest\Commentable\CommenterTrait;
    
    // ...
}

class Post extends Model
{
    use \Namest\Commentable\CommentableTrait;
    
    // ...
}

步骤 6:阅读下面的 API 并开始 happy

API

$user = \App\User::find(1);
$post = \App\Post::find(1);

$comment = $user->comment('Awesome')->about($post); // Return \Namest\Commentable\Comment instance
$users = \App\User::wasCommentedOn($post); // Return all user was commented on a post
$posts = \App\Post::hasCommentBy($user); // Get all posts that the user was leave comment on
$comments = \Namest\Commentable\Comment::by($user); // Return all comments that the user was leave
$comments = \Namest\Commentable\Comment::by($user, 'App\Post'); // Same as above but filter only comments on posts
$user->comments; // Return all comments that the user was leave
$user->commentables; // Return all commentable (in all types)
$post->commenters; // Return all commenters (in all types)

审查

config/commentable.php 文件中设置审查选项

事件

namest.commentable.prepare

何时:在 $commenter 准备在可评论的项上留下评论但事件 namest.commentable.commenting 发生之前

负载

  • $commenter:执行此操作的人
  • $message:评论信息

使用

\Event::listen('namest.commentable.prepare', function ($commenter, $message) {
    // Do something
});

namest.commentable.commenting

何时:在 $commenter 在可评论的项上留下评论之后,但事件 namest.commentable.prepare 之前

负载

  • $commentable:接收评论的项
  • $message:评论信息

使用

\Event::listen('namest.commentable.commenting', function ($commentable, $message) {
    // Do something
});

namest.commentable.commented

何时:在 $commenter 在可评论的项上留下评论之后

负载

  • $comment:评论实例(您可以通过 $comment->commenter$comment->commentable 从评论实例获取评论者和可评论的项)

使用

\Event::listen('namest.commentable.commented', function ($comment) {
    // Do something
});