fperdomo/laravel-comments

Add comments to your laravel model

v0.0.2 2025-06-25 04:01 UTC

This package is auto-updated.

Last update: 2025-06-25 04:02:31 UTC


README

A lightweight Laravel package to add comments and replies to any Eloquent model using a simple use HasComments trait.

✨ Features

  • Add comments to any model
  • Nested comment replies
  • Retrieve all comments or threaded replies
  • Soft deletes supported
  • Automatically deletes child comments when parent is removed
  • Clean and extensible

📦 Installation

composer require fperdomo/laravel-comments

Setup

  1. Publish Migrations (optional if auto-discovered)
php artisan migrate
  1. Use the HasComments trait in any model:
use Fperdomo\Comments\Traits\HasComments;

class Post extends Model
{
    use HasComments;
}

Usage

➕ Add a Comment

$post = Post::find(1);
$comment = $post->comment("I've got a feeling...");

🧑‍💻 Comment as User

$post->commentAsUser("This is great!", auth()->user()->id);

Reply to a Comment

$reply = $comment->reply("Same here!");

Retrieve Comments

$comments = $post->comments;              // All comments (including replies)
$topLevel = $post->comments()->whereNull('parent_id')->get(); // Only top-level
$replies = $comment->replies;             // Replies to a comment

Update a Comment

$comment->update([
    'original_text' => 'Updated comment text',
]);

Delete a Comment

$comment->delete(); // Will also delete nested replies

📘 License

This package is open-sourced under the MIT license.

🙌 Credits

Developed by Fermin Perdomo

Inspired by Laravel's elegant approach to polymorphic relationships.