fperdomo / laravel-comments
Add comments to your laravel model
v0.0.2
2025-06-25 04:01 UTC
Requires
- php: ^8.4
- illuminate/support: ^12.0
Requires (Dev)
- larastan/larastan: ^3.5
- laravel/pint: ^1.22
- orchestra/testbench: ^10.4
- pestphp/pest: ^3.8
- rector/rector: ^2.1
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
- Publish Migrations (optional if auto-discovered)
php artisan migrate
- 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.