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 alibayat/laravel-commentable
php artisan vendor:publish --provider="AliBayat\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' => [
...
\AliBayat\LaravelCommentable\CommentableServiceProvider::class,
],
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use AliBayat\LaravelCommentable\Commentable;
class Post extends Model
{
use Commentable;
}
use App\Models\User;
use App\Models\Post;
use AliBayat\LaravelCommentable\Comment;
// assuming that we have these variables
$user = User::first();
$post = Post::first();
$commentData = [
'title' => 'comment title (nullable)',
'body' => 'comment body'
];
$post->comment($commentData, $user);
$parentComment = Comment::first();
$childCommentData = [
'title' => 'comment title (nullable)',
'body' => 'comment body'
];
$post->comment($childCommentData, $user, $parentComment);
$comment = Comment::first();
$newData = [
'body' => 'new body of the comment to update'
];
$post->updateComment($comment->id, $newData);
$comment = Comment::first();
$post->deleteComment($comment->id);
$post->comments()->delete();
$comment = Comment::first();
$comment->hasChildren();
$post->commentCount();
$post->allComments(); // shows all comments (including children)
$post->comments(); // shows only top level comments
by default when you create a comment, it will be stored as a deactivated comment, unless you provide an 'active' field and set it to true:
$activeComment = [
'body' => 'comment body',
'active' => true
];
$comment = $post->comment($activeComment, $user);
but you can always change the comment's state of activation by using below methods:
$comment->active();
// returns a boolean indicating the state of operation
$comment->deactivate();
// returns a boolean indicating the state of operation
$postWithComments = Post::with('comments')->get();
// returns a collection of all comments associated with the post
$postWithActiveComments = Post::with('activeComments')->get();
// returns a collection of all active comments associated with the post
$comment = Comments::latest()->first();
$comment->parent;
// returns the comment's parent if available
$comment = Comments::latest()->first();
$comment->children;
// returns the comment's children if available
$comment = Comments::latest()->first();
$comment->ancestors;
// return the comment's ancestors if available
$comment = Comments::latest()->first();
$comment->descendants;
// return the comment's descendants if available
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->comments->toTree();
// returns a collection of the comment's tree structure associated with the post
$post->comments->toFlatTree();
// return a collection of the comment's flat tree structure associated with the post
$comment = $post->comments()->latest()->first();
$comment->saveAsRoot();
// Implicitly change the comment's position to Root
// returns boolean
$comment = $post->comments()->latest()->first();
$comment->makeRoot()->save();
// Explicitly change the comment's position to Root
// returns boolean