akira/laravel-commentable

Commentable is a lightweight and flexible comment system package designed to seamlessly integrate into any Laravel project. Whether you’re building a blog, a forum, or a platform like DevHunter – this package makes it incredibly easy to make any model commentable.

Fund package maintenance!
Akira

Installs: 497

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/akira/laravel-commentable

0.1.0 2025-12-23 14:36 UTC

This package is auto-updated.

Last update: 2025-12-23 14:36:34 UTC


README

Latest Version on Packagist Total Downloads PHPStan Level License

A lightweight and flexible comment system for Laravel applications. Add threaded comments, replies, reactions, and moderation to any Eloquent model with minimal configuration.

Features

  • Polymorphic Comments - Make any model commentable with a single trait
  • Threaded Replies - Support for unlimited nested reply depth
  • Reaction System - Built-in support for likes, reactions, and custom types
  • Comment Moderation - Approval workflow for managing user-generated content
  • Flexible Authorization - Customizable permission logic for comment operations
  • Type Safe - Full PHP 8.4 type declarations and PHPStan level 9 compliance
  • Well Tested - Comprehensive test coverage with Pest PHP
  • Developer Friendly - Clean API with extensive documentation

Requirements

  • PHP 8.4 or higher
  • Laravel 12.0 or higher

Installation

Install the package via Composer:

composer require akira/laravel-commentable

Publish and run the migrations:

php artisan vendor:publish --tag="commentable-migrations"
php artisan migrate

Optionally, publish the configuration file:

php artisan vendor:publish --tag="commentable-config"

Quick Start

Make a Model Commentable

Add the Commentable trait to any model that should receive comments:

use Akira\Commentable\Concerns\Commentable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Commentable;
}

Make a Model a Commenter

Add the Commenter trait to models that can create comments (typically your User model):

use Akira\Commentable\Concerns\Commenter;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Commenter;
}

Create Comments

$user = User::find(1);
$post = Post::find(1);

// Create a comment
$comment = $user->comment($post, 'This is a great post!');

// Reply to a comment
$reply = $user->reply($comment, 'Thanks for your feedback!');

// Nested replies
$nestedReply = $user->reply($reply, 'You are welcome!');

Retrieve Comments

// Get all comments on a post
$comments = $post->comments;

// Get only approved comments
$approvedComments = $post->comments()->where('approved', true)->get();

// Get replies to a comment
$replies = $comment->replies;

// Get the commenter
$commenter = $comment->commenter;

Delete Comments

// Delete own comment (with authorization check)
try {
    $user->deleteComment($comment);
} catch (\Akira\Commentable\Exceptions\DeleteCommentNotAllowedException $e) {
    // Handle unauthorized deletion
}

// Force delete (bypass authorization)
$user->forceDeleteComment($comment);

Add Reactions

use Akira\Commentable\Models\Reaction;

// Create a reaction
Reaction::create([
    'comment_id' => $comment->id,
    'owner_type' => User::class,
    'owner_id' => $user->id,
    'type' => 'like',
]);

// Get reactions
$reactions = $comment->reactions;

Documentation

Comprehensive documentation is available at:

https://packages.akira-io.com/packages/laravel-commentable

Documentation Topics

Use Cases

Laravel Commentable is perfect for:

  • Blog comment systems
  • Forum discussions
  • Product reviews and ratings
  • Ticket and support systems
  • Social media platforms
  • Documentation feedback
  • Q&A platforms
  • Community engagement features

Testing

Run the test suite:

composer test

Run individual test types:

composer test:lint          # Code style
composer test:types         # Static analysis
composer test:coverage      # Test coverage
composer test:type-coverage # Type coverage

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

We welcome contributions! Please see CONTRIBUTING for details on how to contribute to this project.

Security Vulnerabilities

If you discover a security vulnerability, please review our Security Policy for information on how to report it responsibly.

Credits

License

The MIT License (MIT). Please see License File for more information.