namest/commentable

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

This package is not auto-updated.

Last update: 2024-05-11 15:09:35 UTC


README

Provide an elegant way to interact with comment feature between your eloquent models.

Note: The package is only support Laravel 5

Installation

Step 1: Install package

composer require namest/commentable

Step 2: Register service provider in your config/app.php

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

Step 3: Publish package resources, include: configs, migrations. Open your terminal and type:

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

Step 4: Migrate the migration that have been published

php artisan migrate

Step 5: Use some traits to make awesome things

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

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

Step 6: Read API below and start 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)

Censoring

Set up censoring options in config/commentable.php file

Events

namest.commentable.prepare

When: Parepare for $commenter to leave a comment on the commentable and but before event namest.commentable.commenting

Payloads:

  • $commenter: Who do this action
  • $message: Comment message

Usage:

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

namest.commentable.commenting

When: Before the $commenter leave a comment on the commentable but after the event namest.commentable.prepare

Payloads:

  • $commentable: Which receive a comment from commenter
  • $message: Comment message

Usage:

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

namest.commentable.commented

When: After the $commenter left a comment on the commentable

Payloads:

  • $comment: Comment instance (you can get commenter & commentable from comment instance by $comment->commenter & $comment->commentable)

Usage:

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