x-laravel / commentable
Polymorphic comment system for Laravel Eloquent models with nested comment support.
v1.0.0
2026-04-11 00:56 UTC
Requires
- php: ^8.2
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^11.0|^12.0
This package is auto-updated.
Last update: 2026-04-12 07:08:28 UTC
README
A Laravel package that adds a polymorphic, infinitely nested comment system to any Eloquent model.
How It Works
- Any model can receive comments via the
Commentabletrait - Any model can post comments via the
Commentertrait - Replies can be nested to any depth
- Replies are lazy-loaded — fetch only what you need, when you need it
- Commenters are optional — anonymous comments are supported
Requirements
- PHP ^8.2
- Laravel ^12.0 | ^13.0
Installation
composer require x-laravel/commentable
Run the migration:
php artisan migrate
Setup
Receiving comments
Add Commentable to any model that should receive comments:
use Illuminate\Database\Eloquent\Model; use XLaravel\Commentable\Commentable; class Post extends Model { use Commentable; }
Posting comments
Add Commenter to any model that should be able to post comments:
use Illuminate\Database\Eloquent\Model; use XLaravel\Commentable\Commenter; class User extends Model { use Commenter; }
Usage
Adding a comment
// Anonymous $post->addComment('Great post!'); // With a commenter $post->addComment('Great post!', $user); // Via Commenter $user->comment($post, 'Great post!');
Adding a reply
// Via Commentable $post->addReply($comment, 'I agree!', $user); // Via Commenter $user->reply($post, $comment, 'I agree!');
Replies can be added to any comment or reply. There is no depth limit.
Listing root comments
$post->rootComments()->paginate(20);
Chain relations as needed:
$post->rootComments()->withCount('replies')->with('commenter')->paginate(20);
Listing replies (lazy-load)
$comment->replies()->paginate(10);
$comment->replies()->withCount('replies')->with('commenter')->paginate(10);
Other relations
// All comments on a model (root + replies) $post->comments()->get(); // All comments posted by a user $user->comments()->get(); // The parent of a reply $comment->parent; // The model a comment belongs to $comment->commentable; // Check if a comment is a reply $comment->isReply(); // true / false
Soft deleting
$comment->delete(); // soft delete $comment->restore(); // restore $comment->forceDelete(); // permanent delete (cascades to replies via FK)
Soft-deleting a parent comment does not cascade to its replies. If you want cascade-on-soft-delete behaviour, add a
deletingevent listener to your application.
Suggested API Design
GET /posts/{post}/comments → List root comments (paginated)
POST /posts/{post}/comments → Add a comment
GET /comments/{comment}/replies → List replies (paginated)
POST /comments/{comment}/replies → Add a reply
DELETE /comments/{comment} → Soft delete a comment
Database
comments
├── id
├── commentable_type (polymorphic — Post, Event, etc.)
├── commentable_id
├── commenter_type (nullable polymorphic — User, Admin, etc.)
├── commenter_id
├── parent_id (nullable FK → comments.id, cascadeOnDelete)
├── body
├── created_at
├── updated_at
└── deleted_at (soft delete)
Testing
# Build first (once per PHP version) DOCKER_BUILDKIT=0 docker compose --profile php82 build # Run tests docker compose --profile php82 up docker compose --profile php83 up docker compose --profile php84 up docker compose --profile php85 up
License
This package is open-sourced software licensed under the MIT license.