sohrab-az/laravel-comment

A flexible comment package for Laravel

Maintainers

Package info

github.com/sohrab-az/laravel-comment

pkg:composer/sohrab-az/laravel-comment

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-05-05 16:23 UTC

This package is auto-updated.

Last update: 2026-05-05 16:25:29 UTC


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 layer
  • HasComments โ†’ model integration layer
  • Comment โ†’ core entity model
  • NestedSet โ†’ 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.