webudvikleren / laravel-commentable
Easily add comments to any Laravel model
Package info
github.com/Webudvikleren/laravel-commentable
pkg:composer/webudvikleren/laravel-commentable
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0|^12.0
README
Laravel Commentable adds a simple way to allow comments on any Eloquent model.
Features
- Add comments to any Eloquent model
- Polymorphic relationship
- User tracking (via
user_id) - Publishable migration and config
- Clean and extensible architecture
Installation
composer require webudvikleren/laravel-commentable
Publish the migration (optional)
php artisan vendor:publish --tag=commentable-config php artisan migrate
If you don’t publish the migration, Laravel will auto-load it from the package.
Usage
Step 1: Add the trait to your model
use Webudvikleren\Commentable\Traits\Commentable; class Post extends Model { use Commentable; }
Step 2: Add comments
$post = Post::find(1); $post->comments()->create([ 'body' => 'This is a comment.', 'user_id' => auth()->id(), ]);
Step 3: Access comments
foreach ($post->comments as $comment) { echo $comment->body; }
Comment Model
By default, comments are stored in the comments table and use the provided Webudvikleren\Commentable\Models\Comment model.
You can override this in your app if needed by extending the model and binding it in a service provider.
Configuration
You can publish the config file to override defaults:
php artisan vendor:publish --tag=commentable-config
Example config/commentable.php (coming soon).
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.