editmode / laravel-comments
This is my package laravel-comments
Fund package maintenance!
Nika
Requires
- php: ^8.4
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
README
A customizable Laravel package to easily add comments and threaded discussions to any model — ideal for blog posts, user profiles, product reviews, or any other entity in your app.
Supports optional like/dislike reactions, nested replies, and is designed to integrate smoothly with Inertia/React or Blade.
Table of Contents
- Installation
- Quickstart
- Config
- Like/Dislike Feature
- Migrations
- Routes
- Development Tips
- Testing
- Changelog
- Credits
- License
Installation
You can install the package via composer:
composer require editmode/laravel-comments
Quickstart
After installing the package, it's recommended that you create your own custom Comment
model that extends the base model provided by the package. This gives you full flexibility to modify, extend, or customize comment behavior as needed.
1. Install the package
composer require editmode/laravel-comments
2. Publish the configuration file (optional but recommended):
php artisan vendor:publish --tag=comments-config
2.5. Publish the migrations
The package includes migrations to create the comments and comment reactions tables.
You should publish them using:
php artisan vendor:publish --tag="comments-migrations"
🛑 Important: If you plan to use the like/dislike feature, make sure to also publish the config file first and enable it in
config/comments.php
by settinglike_dislike_feature
totrue
. Otherwise, thecomment_reactions
table will not be created during migration.
3. Create your own Comment model by extending the base comment model:
🚨 VERY IMPORTANT
For Laravel's polymorphic relationships to work automatically without manual configuration, your custom model must be namedComment
. If you use any other name (likeUserComment
,PostComment
, etc.), morph types likecommentable_type
will not resolve correctly unless you manually configure a morph map. Laravel automatically handles this when your model is namedComment
.
use Nika\LaravelComments\Models\Comment as BaseComment; class Comment extends BaseComment ↑ { // Add your own relationships, scopes, or overrides here }
4. Update your configuration file config/comments.php
to use your custom Comment model:
return [ 'model' => App\Models\Comment::class, ];
This setup allows you to fully control how comments behave in your application while leveraging the core features provided by the package.
5. Attach a comment to a model
You can now attach comments to any model by using the HasComments
trait on that model.
use Nika\LaravelComments\Traits\HasComments; class Post extends Model { use HasComments; }
Then, to add a comment as the currently authenticated user:
$post = Post::find(1); $post->comment('This is a comment from the logged-in user');
Or explicitly pass a user:
$post->commentAsUser($user, 'This is a comment from a specific user');
This allows you to associate comments with any model that uses the HasComments
trait.
Config
You can publish the config file with:
php artisan vendor:publish --tag="comments-config"
Like/Dislike Feature
You can optionally enable the like/dislike feature in the package config. To enable this feature:
- Set
like_dislike_feature
totrue
inconfig/comments.php
. - Add the
HasReactions
trait to your custom comment model.
use Nika\LaravelComments\Models\Comment as BaseComment; use Nika\LaravelComments\Traits\HasReactions; class Comment extends BaseComment { use HasReactions; }
Migrations
You can publish and run the migrations with:
php artisan vendor:publish --provider="Nika\LaravelComments\LaravelCommentsServiceProvider" --tag="comments-migrations"
Then run the migrations:
php artisan migrate
⚠️ The
create_comment_reactions_table
migration will be skipped unless thelike_dislike_feature
setting is enabled in yourconfig/comments.php
file. If you plan to use the like/dislike feature, make sure to publish the config first and setlike_dislike_feature
to true before runningphp artisan migrate
.
Routes
You can register the package's routes by calling the macro in your routes/web.php
:
Route::comments();
This will automatically register routes like GET /comments
, and if the like/dislike feature is enabled,
also POST /comments/{comment}/react/{type}
— where {type}
must be either like
or dislike
.
By default, routes are prefixed with /comments
. You can change this by passing a different base URL
to Route::comments('your-prefix')
.
💡 See Development Tips for filtering the route list.
Published Routes:
Method | URI | Name |
---|---|---|
GET | /{prefix} |
— |
POST | /{prefix} |
comment.store |
PATCH | /{prefix}/{comment} |
comment.update |
DELETE | /{prefix}/{comment} |
comment.destroy |
POST | /{prefix}/{comment}/react/{type} |
comment.reaction.toggle |
Development Tips
Filtering Routes
To list only the routes registered by this package:
php artisan route:list --path=comments
This helps when debugging or inspecting how the package integrates into your app.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.