sohrab-az / laravel-comment
A flexible comment package for Laravel
1.0.0
2026-05-05 16:23 UTC
Requires
- kalnoy/nestedset: ^7.0
Requires (Dev)
- orchestra/testbench: ^11.1
README
A lightweight and flexible comment system for Laravel with support for nested replies, polymorphic relations, and status workflow.
โจ Features
- Nested comments (threaded replies)
- Polymorphic support (commentable)
- User or guest commenting
- Status workflow (pending, approved, rejected)
- Configurable max reply depth
- Clean service layer (CommentManager, CommentQuery)
- Fluent query builder for comments
- Simple trait integration
๐ฆ Installation
Install via Composer:
composer require sohrab-azinfar/comment
โ๏ธ Publish Config & Migrations
php artisan vendor:publish --tag=comment-config php artisan migrate
โ๏ธ Configuration
config/comment.php
return [ 'default_status' => \SohrabAzinfar\Comment\Enums\CommentStatusEnum::Pending->value, // Maximum depth of nested replies 'depth' => 5, ];
๐ง Usage
Add Trait to your Model
use SohrabAzinfar\Comment\Traits\HasComments; class Post extends Model { use HasComments; }
๐ฌ Creating Comments
Create comment
$post->addComment( body: 'This is a comment', author: auth()->user() );
Guest comment
$post->addComment( body: 'Hello world', author: null, guest: [ 'name' => 'John', 'email' => 'john@example.com' ] );
๐ Reply to a Comment
$post->replyTo($comment, 'This is a reply', auth()->user());
๐ Query Comments
$comments = $post->queryComment() ->approved() ->roots() ->withReplies() ->get();
Pagination
$comments = $post->queryComment()->paginate(10);
๐งพ Comment Status
use SohrabAzinfar\Comment\Enums\CommentStatusEnum; CommentStatusEnum::Pending; CommentStatusEnum::Approved; CommentStatusEnum::Rejected;
โ๏ธ Approve / Reject
$post->approve($comment); $post->reject($comment);
๐งน Delete Comment (with descendants)
$post->deleteComment($comment);
๐งฑ Database Structure
The package creates a comments table with:
- Polymorphic relation (
commentable) - Optional author morph (
author) - Nested set support (tree structure)
- Status management
- Soft-style timestamps
๐ง Architecture
CommentManagerโ write operations (create, reply, approve, reject, delete)CommentQueryโ read/query layerHasCommentsโ model integration layerCommentโ core entity modelNestedSetโ tree handling (Kalnoy package)
๐ Example
$post = Post::find(1); $comment = $post->addComment('Nice post!', auth()->user()); $post->replyTo($comment, 'Thanks!');
๐ Requirements
- PHP 8.1+
- Laravel 10+
๐ License
MIT License. Use freely in personal and commercial projects.