A Laravel package that adds a polymorphic, infinitely nested comment system to any Eloquent model.
- 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
- PHP ^8.2
- Laravel ^12.0 | ^13.0
composer require x-laravel/commentableRun the migration:
php artisan migrateAdd Commentable to any model that should receive comments:
use Illuminate\Database\Eloquent\Model;
use XLaravel\Commentable\Commentable;
class Post extends Model
{
use Commentable;
}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;
}// Anonymous
$post->addComment('Great post!');
// With a commenter
$post->addComment('Great post!', $user);
// Via Commenter
$user->comment($post, 'Great post!');// 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.
$post->rootComments()->paginate(20);Chain relations as needed:
$post->rootComments()->withCount('replies')->with('commenter')->paginate(20);$comment->replies()->paginate(10);$comment->replies()->withCount('replies')->with('commenter')->paginate(10);// 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$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.
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
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)
# 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 upThis package is open-sourced software licensed under the MIT license.