This Package makes it easy to implement Commenting system for Eloquent's Models. just use the trait in the model and you're good to go.
- PHP 7.2+
- Laravel 7+
composer require babakhakimi/laravel-commentable
php artisan vendor:publish --provider="BabakHakimi\LaravelCommentable\CommentableServiceProvider"
php artisan migrate
Laravel Commentable package will be auto-discovered by Laravel. and if not: register the package in config/app.php providers array manually.
'providers' => [
...
\BabakHakimi\LaravelCommentable\CommentableServiceProvider::class,
],
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use BabakHakimi\LaravelCommentable\Commentable;
class Post extends Model
{
use Commentable;
}
use App\User;
use App\Post;
use BabakHakimi\LaravelCommentable\Comment;
// assuming that we have these variables
$user = User::first();
$post = Post::first();
$comment =
[
'title' => 'comment title (nullable)',
'body' => 'comment body'
];
$commentId = 1;
$post->comment($comment, $user);
$post->comment($comment, $user, $parent);
$comment = Comment::first();
$comment->hasChildren();
$post->updateComment($commentId, $comment);
$post->deleteComment($commentId);
$post->commentCount();
by default when you add a cooment it is stored as a deactive comment, unless you provide an 'active' field and set it to true:
$activeComment =
[
'title' => 'comment title (nullable)',
'body' => 'comment body',
'active' => true
];
$post->comment($activeComment, $user);
but you can always change the comment state by using below methods:
$post->active();
// returns true if operation is successful
$post->deactive();
// returns true if operation is successful
$postWithComments = Post::with('comments')
->get();
// return all comments associated with the post
$postWithActiveComments = Post::with('activeComments')
->get();
// return all active comments associated with the post
$comment = Post::first()->comments()->first();
$comment->parent;
// return the comment's parent if available
$comment = Post::first()->comments()->first();
$comment->children;
// return the comment's children if any
$comment = Post::first()->comments()->first();
$comment->ancestors;
// return the comment's ancestors if any
$comment = Post::first()->comments()->first();
$comment->descendants;
// return the comment's descendants if any
thanks to the great laravel-nestedset package, you have access to some additional functionalities, we review some of them here but you can always refer to the package's repository for the full documentation.
Post::first()->comments->toTree();
// return a collection of the comment's tree structure
Post::first()->comments->toFlatTree();
// return a collection of the comment's flat tree structure
$comment = Post::first()->comments()->latest()->first();
$comment->saveAsRoot();
// Implicitly change the comment's position to Root
// return bool
$comment = Post::first()->comments()->latest()->first();
$comment->makeRoot()->save();
// Explicitly change the comment's position to Root
// return bool
- Babak Hakimi - [email protected]